views:

88

answers:

1

hi folks

i have a mod_rewrite redirection problem i cannot figure out.

all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:

RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]

however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]    

i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?

btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.

many thanks for any pointers.

+1  A: 

Inspect the HTTP request line in THE_REQUEST instead:

RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]
Gumbo
thanks gumbo! using THE_REQUEST circumvents the loop.however, the RewriteRule line i had to change to "RewriteRule ^(.*) /$1 [L,R=301]", because the .htaccess file is executed from the subdir "net".
halloleo