trailing slash on rewrite problem

Topics: User Forum
Nov 24, 2006 at 1:03 AM
hi i want if someone requests: http://subdomain.domain.tld/directory
The webserver (iis 6) will serve: http://domain.tld/static_vdir/subdomain/directory

I used this:

RewriteCond %{HTTP_HOST} (.+?)\.domain\.tld
RewriteRule (.+) /static_vdir/%1$1 L

While this works for http://subdomain.domain.tld/directory/
It doesnt work for http://subdomain.domain.tld/directory - that gets actually redirected (i think through content-location header in response) to:
http://subdomain.domain.tld/staticvdir/subdomain/staticvdir/subdomain/directory/ which doesnt exist

Any help? I find this a bit confusing, I thought it would redirect only once to the right directory.
Nov 24, 2006 at 2:01 AM
ok i have realised why, when the webserver receives a request for a non trailing slash it checks to see if there exists a file, if not it will return 301 moved permanently to the same path with a slash, in my case rewrite was getting that re-request and rewriting that aswell....

only way i can think of is to rewrite things that look like directories to have trailing slashes, and anything else as normal:

RewriteCond %{HTTP_HOST} (.+?)\.domain\.tld
RewriteRule (.+?\.\w{3,4}$) /static_vdir/%1$1 L
RewriteCond %{HTTP_HOST} (.+?)\.domain\.tld
RewriteRule (.+?)(^\./{4,5})?/?$ /static_vdir/%1$1/ L

If anyone has better please submit, I would rather do it in one rule!
Dec 11, 2006 at 3:19 PM
I had a similar issue. I wanted to have certain domain names forwarded while others, not. I found an issue with this rewrite rule posted above because if you visit: http://subdomain.domain.tld/index.html

it will NOT work (it's parsing another . in there somewhere)

Here is how I do it.

RewriteCond %{HTTP_HOST} (sub1|sub2|sub3|sub4|sub5|etc|forums|cms)\.domain\.tld
RewriteRule (.+?\.\w{3,4}$) /static_vdir/%1$1$2$3 L
RewriteCond %{HTTP_HOST} (sub1|sub2|sub3|sub4|sub5|etc|forums|cms)\.domain\.tld
RewriteRule (.+?)(^\./{4,5})?/?$ /static_vdir/%1$1$2$3/ L

This way, any subdomain in the list (sub1++) will be rewritten while subdomains like mail.domain.tld and www.domain.tld will not.