This code works perfectly in .htaccess or directory
RewriteEngine on
RewriteCond %{request_uri} !^/dir/$ [NC]
RewriteRule ^(.*)$ /dir/ [END]
but when put into VirtualHost it causes infinite redirection.
I've read documentation about diffrences between request_uri in scope of virtualhost and directory but I'm still unable to produce code that will work in virtualhost context. What should I change?
I think you probably have a conflict with mod_dir (and the
DiretcoryIndex
). Although I'm surprised this "works perfectly in.htaccess
" (unless you have something else that prevents a loop)?If you rewrite to the bare directory, ie.
/dir/
then mod_dir will trigger an internal subrequest to/dir/index.php
(or whatever yourDirectoryIndex
is set to) and this will likely trigger an endless loop (despite yourEND
flag) since/dir/index.php
matches!^/dir/$
.You should be rewriting directly to the index document and this should solve the problem. For example:
You don't need the
RewriteCond
directive that checks againstREQUEST_URI
, you can do the same check in theRewriteRule
pattern - which is marginally more efficient and saves you a directive.