Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to redirect all traffic to example.com to www.example.com, and then rewrite everything to /.

So if a visitor by some reason hits http://example.com/foo/bar I want to redirect them to http://www.example.com/foo/bar 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 (.*) http://www.example.com$1 [R=301,L]

EDIT:

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 (.*) http://www.example.com$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 http://www.example.com/home -- 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.

Anyone?

Update:

I'm seeing this in the apache error log: [Fri Mar 25 04:33:45 2011] [error] [client 123.123.123.123] 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 (.*) http://www.example.com$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
</Location>
share|improve this question
add comment

1 Answer 1

add

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
add comment

Your Answer

 
discard

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.