mod_rewrite with trailing period in URL

前端 未结 2 553
广开言路
广开言路 2021-01-07 13:59

I have a RewriteRule on my Apache to make URLs friendly

RewriteRule ^log/(.+)$ script.php?u=$1 [QSA] 

This makes http://example.com/log/use

2条回答
  •  一生所求
    2021-01-07 14:26

    You can try to check these configuration directives:

    RewriteCond %{REQUEST_URI} !^/log/(.+)\.$
    RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
    
    RewriteCond %{REQUEST_URI} ^/log/(.+)\.$
    RewriteRule ^log/(.+)$ /script.php?u=$1. [QSA]
    

    OR:

    RewriteCond %{REQUEST_URI} ^/log/(.+)([a-zA-Z0-9_-]{1})$
    RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
    
    RewriteCond %{REQUEST_URI} ^/log/(.+)\.$
    RewriteRule ^log/(.+)$ /script.php?u=$1. [QSA]
    

    I hope, I'm not mistaken.. Now, as the variable of usernames with one character or one character with trailing period won't work in the second code, we can try to replace the second code with this one:

    RewriteCond %{REQUEST_URI} ^/log/(.*)([a-zA-Z0-9_-]{1})$
    RewriteRule ^log/(.+)$ /script.php?u=$1 [QSA]
    
    RewriteCond %{REQUEST_URI} ^/log/(.*)\.$
    RewriteRule ^log/(.*)$ /script.php?u=$1. [QSA]
    

    Or you could try these three liner directives if you doesn't like the four liner:

    RewriteCond %{REQUEST_URI} ^/log/(.*)\.$
    RewriteRule ^log/(.*)$ /script.php?u=$1. [QSA]
    RewriteRule ^log/(.*)([a-zA-Z0-9_-]{1})$ /script.php?u=$1$2 [QSA]
    

    I hope, all these codes above will work!

提交回复
热议问题