rewrite uppercase url to lowercase url htaccess

2019-07-18 17:07发布

问题:

I want my urls that contain uppercase characters to be redirected to lowercase url's. Unfortunatly i don't have access to the httpd.config file. So i tried this method to rewrite the urls with the htaccess file: http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html . But it adds extra / because it's a loop and also it doesn't work (504 error) when the url consists of more than two parts (ex www.mysite.com/part1/part2/part3/ )

How can i fix this or is there an other/better method?

回答1:

If you look at your Apache error log (which is most likely huge thanks to this thing), you should see something along the lines of:

[Sun Dec 08 09:46:28.884713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] strip per-dir prefix: C:/wamp/www/AAA/AA -> AAA/AA
[Sun Dec 08 09:46:28.884713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] applying pattern '[A-Z]' to uri 'AAA/AA'
[Sun Dec 08 09:46:28.884713 2013] [rewrite:trace5] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] setting env variable 'HASCAPS' to 'TRUE'
[Sun Dec 08 09:46:28.884713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] add path info postfix: C:/wamp/www/AAA -> C:/wamp/www/AAA/AA
[Sun Dec 08 09:46:28.885713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] strip per-dir prefix: C:/wamp/www/AAA/AA -> AAA/AA
[Sun Dec 08 09:46:28.885713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] applying pattern '^([^A]*)A(.*)$' to uri 'AAA/AA'
[Sun Dec 08 09:46:28.885713 2013] [rewrite:trace2] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] rewrite 'AAA/AA' -> 'aAA/AA'
[Sun Dec 08 09:46:28.885713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] add per-dir prefix: aAA/AA -> C:/wamp/www/aAA/AA
[Sun Dec 08 09:46:28.885713 2013] [rewrite:trace3] [pid 4860:tid 1532] mod_rewrite.c(468): [client ::1:50143] ::1 - - [localhost/sid#56cb48][rid#1642140/initial] [perdir C:/wamp/www/] add path info postfix: C:/wamp/www/aAA/AA -> C:/wamp/www/aAA/AA/AA

Everything goes well, until the path info postfix is applied. I am not sure why that is, but I know that applying the DPI flag to the 26 RewriteRules that transform uppercase to lowercase will most likely solve your issue.

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2 [DPI]
RewriteRule ^([^B]*)B(.*)$ $1b$2 [DPI]
RewriteRule ^([^C]*)C(.*)$ $1c$2 [DPI]
RewriteRule ^([^D]*)D(.*)$ $1d$2 [DPI]
RewriteRule ^([^E]*)E(.*)$ $1e$2 [DPI]
RewriteRule ^([^F]*)F(.*)$ $1f$2 [DPI]
RewriteRule ^([^G]*)G(.*)$ $1g$2 [DPI]
RewriteRule ^([^H]*)H(.*)$ $1h$2 [DPI]
RewriteRule ^([^I]*)I(.*)$ $1i$2 [DPI]
RewriteRule ^([^J]*)J(.*)$ $1j$2 [DPI]
RewriteRule ^([^K]*)K(.*)$ $1k$2 [DPI]
RewriteRule ^([^L]*)L(.*)$ $1l$2 [DPI]
RewriteRule ^([^M]*)M(.*)$ $1m$2 [DPI]
RewriteRule ^([^N]*)N(.*)$ $1n$2 [DPI]
RewriteRule ^([^O]*)O(.*)$ $1o$2 [DPI]
RewriteRule ^([^P]*)P(.*)$ $1p$2 [DPI]
RewriteRule ^([^Q]*)Q(.*)$ $1q$2 [DPI]
RewriteRule ^([^R]*)R(.*)$ $1r$2 [DPI]
RewriteRule ^([^S]*)S(.*)$ $1s$2 [DPI]
RewriteRule ^([^T]*)T(.*)$ $1t$2 [DPI]
RewriteRule ^([^U]*)U(.*)$ $1u$2 [DPI]
RewriteRule ^([^V]*)V(.*)$ $1v$2 [DPI]
RewriteRule ^([^W]*)W(.*)$ $1w$2 [DPI]
RewriteRule ^([^X]*)X(.*)$ $1x$2 [DPI]
RewriteRule ^([^Y]*)Y(.*)$ $1y$2 [DPI]
RewriteRule ^([^Z]*)Z(.*)$ $1z$2 [DPI]

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R,L]

More information about the DPI flag can be found here and the general documentation of mod_rewrite can be found here.