mod_rewrite with trailing period in URL

前端 未结 2 554
广开言路
广开言路 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:02

    Your rule appears to be right, perhaps the client or server is stripping the last dot as spurious (checked on YT with Chrome, if you add a dot at the end of a video url (?=xxxxxxxxx to ?=xxxxxxxxx.) and press enter, it gets removed -- actually triggering a 303 HTTP response).

    In general, you should use only upper/lowercase letters and non-trailing dashes or dots in urls to compose the so-called slug, which is guaranteed to be handled correctly across all decent browsers and web servers.

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题