Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a tricky problem related to 301 redirections I badly need help with. I tried to implement these via .htaccess, but ran into trouble.

The start of my .htaccess looks like this:

SetEnv PHP_VER 5
Options +FollowSymlinks
RewriteEngine on

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#--- GENERAL ---
RewriteRule ^index\.html$ index.php [L]
...

When I try to put a permanent redirect to index.php by adding R=301 in the square brackets, I get a 404, and I have no idea where the error comes from.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

To force www. to be present in a domain name try this one instead (it's safer to have domain name hard coded):

RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule (.*) http://www.example.com/$1 [QSA,R=301,L]

A bit different version (with no domain name hard coded):

RewriteCond %{HTTP_HOST} !^www\.(.*)$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [QSA,R=301,L]

But if your configuration allows to to also accept requests for domain like meow.example.com .. then it will be redirected to www.meow.example.com.

share|improve this answer
    
The problem is with the index.php redirect when I try to make it permanent, not with the not-www to www. I just put this one there in case it may have an impact. –  Argoron Jun 26 '11 at 12:54
    
@Argoron I see. It should not really matter .. but try adding / before index.php. Maybe you need to add RewriteBase / after RewriteEngine on line. Also ... what other rules do you have there (as they may be rewriting it further)? Can you post them all? Also ... are you able to reach it via http://www.doamin.com/index.php ? –  LazyOne Jun 26 '11 at 13:02
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.