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.


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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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