Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I keep getting variations of the following answer, and it doesn't work

RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$$1 [R=301,L]

The reason this solution is no good is that if I go to

I'll never get be redirected to the

Can anyone provide the correct rewrite required here?

share|improve this question

closed as off topic by ChrisF, Ƭᴇcʜιᴇ007, Sathya Jun 2 '11 at 4:53

Questions on Super User are expected to relate to computer software or computer hardware within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Already answered at length, for rewriting in both directions, on ServerFault and StackOverflow over and over and over and over and over and over and over and over and over again. – JdeBP Jun 1 '11 at 12:57
Wow man I had to plus one your comment as it is amazing how much effort you put into that, finding all of them and linking them like that. Love it! – Jason Jun 2 '11 at 1:08

I'd suggest this:

RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^/(.*)$$1 [R=301,L]

The key part is the '/' after the '^' in the second line. The NC in means it'll work if someone types in mixed/upper case.

share|improve this answer

This is such a common question that it's part of the documentation for mod_rewrite - which you did read, right?

Assuming you're running on port 80:

RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)$1 [L,R]

the documentation has another example in case you're not on port 80, and covers other common scenarios

share|improve this answer

Not the answer you're looking for? Browse other questions tagged .