Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 want to redirect all traffic to to, and then rewrite everything to /.

So if a visitor by some reason hits I want to redirect them to and then rewrite that request to /index.php

This is what I've got so far (the non-www to www redirect works):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^11\.22\.33\.44
RewriteRule (.*)$1 [R=301,L]


Okay I've got it to work with this:

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule (.*)$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

BUT the strangest thing: Apache does something weird with -- it's just like apache has some internal routing for this path because it bypasses my vhost conf and sends its own 404. And there isn't any /home in my document root so the -s/-d/-l shouldn't matter. Also I've grep:ed for home in /etc/apache2 without any hits.



I'm seeing this in the apache error log: [Fri Mar 25 04:33:45 2011] [error] [client] File does not exist: /var/www/example/web/home

So I guess that the request hits my vhost but isn't caught by my rules?

Update 2:

This is weird. If I removed this line RewriteRule ^.*$ - [NC,L] the request to /home came trough. But only requests to /home.

Instead I ended up with following config, which works:

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^123\.123\.123\.123
RewriteRule (.*)$1 [R=301,L]

<Location />
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !\.(js|ico|gif|jpg|png|css|txt)$ /index.php
share|improve this question
You can easilect www to non www version and from non www version. Check the method at – prasoon sharma May 2 at 17:58


RewriteRule ^(.+)$ index.php [L,QSA]
share|improve this answer
Where should I add that? Is it supposed to replace something? Thanks. – jim Mar 25 '11 at 6:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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