odd rewrite when trailing slash misses

Oh, the strange things mod_rewrite does!

odd rewrite when trailing slash misses

Postby avo » Sun Jun 29, 2008 12:26 am

there's these mod rewrite rules I wrote up over a year ago:

Code: Select all
#loop stopping code.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

#if address is www, redirect to /main/
RewriteCond %{HTTP_HOST} ^www\.web\-site\.net$ [NC]
RewriteRule ^(.*)$ /main/$1 [QSA,L]

#if address is img, redirect to /images/
RewriteCond %{HTTP_HOST} ^img\.web\-site\.net$ [NC]
RewriteRule ^(.*)$ /images/$1 [QSA,L]


#if none of these are the case, execute redirect to /users/
RewriteRule ^(.*)$ /users/$1 [QSA,L]


So, to sum that up, img.web-site.net reads from the /images/ folder, any username on the site (hank.web-site.net) reads from /users/ (username is grabbed from the url with php) and www reads from /main/. and subfolders and the query string are appended...

It works fine with this address: http://www.web-site.net/browse/. however, if you enter http://www.web-site.net/browse (notice, no trailing slash) it redirects to http://www.web-site.net/main/browse/.

which it shouldn't do =\

Does anyone have any ideas on how to fix this?
avo
 
Posts: 1
Joined: Sun Jun 29, 2008 12:15 am

Postby richardk » Mon Jun 30, 2008 12:18 pm

Mod_dir is adding the missing trailing slash, it is adding it to the directory not to the fake URL. You must add it before mod_dir
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Get the right sub dircetory.
RewriteCond %{HTTP_HOST}/main ^www\.web-site\.net(/main)$ [NC,OR]
RewriteCond %{HTTP_HOST}/images ^img\.web-site\.net(/images)$ [NC,OR]
RewriteCond /users ^(/users)$ [NC]
# Check if the directory exists.
RewriteCond %{DOCUMENT_ROOT}%1%{REQUEST_URI}/ -d
# If it doesn't have a trailing slash, add one.
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Stop loops.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# Get the right sub dircetory.
RewriteCond %{HTTP_HOST}/main ^www\.web-site\.net(/main)$ [NC,OR]
RewriteCond %{HTTP_HOST}/images ^img\.web-site\.net(/images)$ [NC,OR]
RewriteCond /users ^(/users)$ [NC]
RewriteRule ^(.*)$ %1/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron