2

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?

1
  • "I've read documentation about diffrences between request_uri in scope of virtualhost and directory" - what have you read? There is no difference in the value of the REQUEST_URI variable between virtualhost and directory context.
    – MrWhite
    Sep 15 '17 at 1:20
0

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 your DirectoryIndex is set to) and this will likely trigger an endless loop (despite your END flag) since /dir/index.php matches !^/dir/$.

You should be rewriting directly to the index document and this should solve the problem. For example:

RewriteEngine on
RewriteRule !^/?dir/$ /dir/index.php [END]

You don't need the RewriteCond directive that checks against REQUEST_URI, you can do the same check in the RewriteRule pattern - which is marginally more efficient and saves you a directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.