I would like to redirect every request to root by using RewriteRules in my .htaccess file (later on I will copy them to a vhost file). I tried something like this:

RewriteRule ^(.*)$ http://test.example.com/ [R=301,L]

This works, but obviously this generates an endless loop. I think I need a RewriteCond that excludes the root. So I tried something like:

RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://test.example.com/ [R=301,L]

But sadly this does not seem to work. I have been searching around for some time know but I only come across RewriteRules that remove or add the slash with every url (which I don't want). Help would be greatly appreciated.

I am using Apache2 + mod_rewrite on a linux machine.


This is probably more a question for serverfault I guess. The root is just a /. So you have to tell it starts with /, ^/, and also that it ends right after, so you have to add a $, leading to the following line.

RewriteCond %{REQUEST_URI} !^/$
  • Doh, I see it now. Thanks it worked! And I thought I created it under StackOverflow, my apologies. Can a mod transfer this question? – Jona Koudijs Nov 12 '15 at 11:16
  • You're welcome. Actually this is a duplicate of this : stackoverflow.com/questions/5437375/… – Pierre-Alain TORET Nov 12 '15 at 12:38
  • You are totally right, I did not found that question in my search for duplicates. – Jona Koudijs Nov 12 '15 at 12:41
  • Actually I think the original post I pasted here has nothing to do on StackOverflow, all this should be on ServerFault as it's part of server administration. – Pierre-Alain TORET Nov 12 '15 at 12:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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