views:

12

answers:

1

I need to change the url of a directory so that:

www.example.com/foo/

becomes

www.example.com

I can't move the files.

Putting a htaccess file using mod_rewrite to simply rewrite www.example.com to www.example.com/foo wasn't a problem

RewriteEngine On
RewriteRule !^foo/ foo%{REQUEST_URI} [L]

However I must ensure that if the user requests www.example.com/foo that the http status is 404 or the user is redirected to www.example.com. Unfortunately whatever I do, it seems to end up in an infinite loop. For example this results in an infinite redirection loop:

RewriteEngine On
RewriteRule !^foo/ foo%{REQUEST_URI} [L]

RewriteCond  %{REQUEST_URI}  ^/foo
RewriteRule  ^foo(.*) http://www.example.com$1 [R=301,L]
+1  A: 

You need to condition on the original request sent to the server, since the %{REQUEST_URI} will change during the mod_rewrite processing, which causes the internal redirect loop.

Consequently, something like this should take care of things (for the 404, your 301 RewriteRule should work fine as well if you want to swap that in):

RewriteCond %{THE_REQUEST} ^[A-Z]+\s/foo [NC]
RewriteRule ^ - [R=404]
Tim Stone