mod_rewrite force lowercase urls
i've searched the net for an answer to my problem without any luck, so her goes:
I'm redoing my mod_rewrite for SEO purposes and i want all urls to be lowercase, for instance:
http://BLAbla.com/
becomes
http://bla.com/
currently my .htaccess looks like this:
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]
but when i run http://localhost/site/ i get error 500. I only know basic reg开发者_StackOverflow ex, and very limited mod_rewrite, so i cannot se the error here. If i remove the rewritemap line, the page doesn't give any error. Is rewritemap not supported by my server or something??
Any other comments regarding the .htaccess file are very very welcome :)
Thanks for your time.
@Esben
Rewriting the hostname is a bit trickier. Do you have access to the server error log? Because that may some using information about the cause of the 500.
It's a good idea to enclose the parameters in quotation marks just for clarity:
RewriteMap lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)" "${lc:$1}" [R=301,L]
The [L]
is going to force this to end processing for this request, which means that if there's more work to be done to the URI, it'll happen when the request is re-submitted. You might want to consider batching your modifications so requests only go through the munging process once:
RewriteMap lc int:tolower
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)" "${lc:$1}" [E=REDIRECT:TRUE]
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteRule "^" "-" [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]
What is the following trying to do?
RewriteRule ([\w]*)/$ http://%{HTTP_HOST}/$1 [R=301,L]
The effect (despite the funky regex) is 'If the REQUEST_URI ends in a '/
', strip it off and redirect.' Is that what you want? Bear in mind that things are a little different in a .htaccess
file..
And this:
RewriteRule ^([^/]+)/([^/]?)$ index.php?worker=$1¶ms=$2 [L]
It looks like you're trying to say 'Change any "foo/bar" into "index.php?worker=foo¶ms=bar"'. If that's correct, it's probably not going to work. Since you're in a .htaccess
file, you're matching against the filesystem path, not the URI.
So try this:
RewriteMap "lc" "int:tolower"
RewriteCond "%{REQUEST_URI}" "[A-Z]"
RewriteRule "(.*)" "${lc:$1}" [E=REDIRECT:TRUE]
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteRule "^" "-" [E=REDIRECT:TRUE,E=PREFIX_WWW:TRUE]
RewriteCond "${ENV:REDIRECT}" "!TRUE"
RewriteRule "^" "-" [SKIP=2]
RewriteCond "${ENV:PREFIX_WWW}" "TRUE"
RewriteRule "(.*)" "http://www.${HTTP_HOST}/$1" [R=301,L]
RewriteRule "^" "-" [R=301,L]
RewriteRule "^([^/]+)/([^/]?)$" "index.php?worker=$1¶ms=$2" [PT]
I'm not sure about the effect of SKIP
on RewriteCond
statements mixed with RewriteRule
s, but give the above a try and see if you're any closer.
精彩评论