Forum Moderators: phranque
as someone relatively new to the delights of mod_rewrite, I would really appreciate it if someone more experienced with this could take a quick look at my code. As far as my tests show it all works, but as it's doing several things (compiled from different examples I found) I want to make sure it's not doing any thing superfluous/there are no obvious redundant bits/there are any glaring bugs etc.
I want to do basically 4 things:
The code is below (I've got it in a .htaccess file).
RewriteEngine On
# Remove leading www from all requests
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Add trailing slash if not present
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
# Redirect to relevant index page in the root
RewriteRule ^(enŠfrŠes) index.$1.html [NC,QSA]
# Redirect to language subsite if cookie set
RewriteCond %{HTTP_cookie} ^lang=(enŠeuŠes)$
RewriteRule ^$ http://example.com/%1/
Any comments/criticisms/suggestions for improvements are greatly appreciated!
[edited by: jdMorgan at 2:23 pm (utc) on Nov. 15, 2007]
[edit reason] example.com [/edit]
RewriteEngine On
RewriteBase /
#
# Externally redirect to add missing trailing slash to non-file URLs
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) http://example.com/$1/ [R=301,L]
#
# Externally redirect to language subsite if cookie set
RewriteCond %{HTTP_COOKIE} ^lang=(enŠeuŠes)$
RewriteRule ^$ http://example.com/%1/ [R=302,L]
#
# Externally redirect to remove leading www from all requests (if not already done by rules above)
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
#
# Internally rewrite to relevant index page in the root
RewriteRule ^(enŠfrŠes)/ index.$1.html [NC,L]
The other important change was to the RewriteCond order in the now-first rule: It is important to avoid the slow and expensive 'file-exists' check unless it is really necessary, so always place these last when using multiple RewriteConds. The other even-slower RewriteCond is doing a %{REMOTE_HOST} check, which requires your server to issue a reverse-DNS lookup request to a (usually-external) DNS server. File and directory exists checks and reverse-DNS checks should therefore be avoided or put off 'til last based on all the other RewriteConds matching.
Other changes were made to eliminate unnecessary, incorrect, and/or redundant regex tokens, and to make the comments more accurate.
I note that you've got (enŠeuŠes) in one rule, and (enŠfrŠes) in the other. These should most likely be consistent.
Change all broken pipe "Š" characters above to solid pipes before use; Posting on this forum modifies the pipe characters.
Jim
I was just testing the script when I noticed something - if there is only the one cookie, then it works fine. However, if there is more than one (the PHP session id for example), then it doesn't redirect to the language subsite. I managed to work out that this was due to the regular expression not matching "lang=en" because the HTTP_COOKIE value contains all the cookie values, each separated by a semicolon and a space. To get this to work, I've added the bits in bold to this line:
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(lang=(enŠeuŠes))(;\ .*)?$
And I had to change the back-reference to "%3" to get the cookie value in this line:
RewriteRule ^$ http://example.com/%3/ [R=302,L]
And it's all fine and dandy now. :-)
Thanks again for all the help,
Best wishes,
Dave