Forum Moderators: phranque
Here is what I have for my root .htaccess:
# Rewrite Rule for machine.forexample-domain.net
RewriteCond %{HTTP_HOST} directory.example.com$
RewriteCond %{REQUEST_URI}!directory/
RewriteRule ^(.*)$ directory/$1
This works somewhat fine in that when you go to directory.example.com it displays the page from example.com/directory/
However, this still allows you to go to example.com/directory/ and I want anybody accessing example.com/directory/ to be redirected to directory.example.com
I tried putting a redirect 301 after the other stuff but that causes an error when you access the page (which sort of makes sense cause it seems a bit like a loop).
Does anyone have any idea how to get this to work?
thanks
john
Use THE_REQUEST to detect /directory requests due to direct client accesses as opposed to those occurring as a result of the internal rewrite. This prevents the rewrite-redirect looping you encountered.
# Rewrite Rule for subdomain.example.com
RewriteCond %{HTTP_HOST} ^directory\.example\.com
RewriteCond %{REQUEST_URI} !^/directory
RewriteRule (.*) /directory/$1 [L]
#
# Prevent direct client access to subdirectory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /directory/
RewriteRule ^directory/(.*)$ http://directory.example.com/$1 [R=301,L]
GET /index.php?cat=wigets&prod=123 HTTP/1.1
As shown above, use the [L] flag on every RewriteRule -- Unless you have a good reason why you don't want to use it.
Jim
[edited by: jdMorgan at 3:05 am (utc) on May 26, 2007]