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 http://www.example.com to http://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 http://www.example.com/foo that the http status is 404 or the user is redirected to http://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]
You need to condition on the original request sent to the server, since the
%{REQUEST_URI}will change during themod_rewriteprocessing, which causes the internal redirect loop.Consequently, something like this should take care of things (for the 404, your 301
RewriteRuleshould work fine as well if you want to swap that in):