I want to point any domain.com/page to the www.domain.com/page without showing index.php in my urls.

I have a www and non-www A record pointing to an IP address.

How do I set up my .htaccess file to do this?

1 Answer 1


Here's what I'm using:

# Remove the www from the URL
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Exclude root files
RewriteCond $1 !^(favicon\.ico|favicon\.png|fb_share\.jpg|apple-touch-icon\.png|apple-touch-icon-precomposed\.png|index\.php|robots\.txt) [NC]

# Exclude asset folders
RewriteCond $1 !^(assets|uploads)/ [NC]

# Remove index.php
RewriteRule ^(.*)$ /index.php/$1 [L]

You could also set up a CNAME record from www.domain.com to domain.com in your DNS.

  • Thanks for the tip. Is there a best practice for pointing non-www to www or vice versa? Do I need the CNAME if I have my A records set up as described in my original question? Sep 4, 2014 at 17:52
  • And should I wrap <IfModule mod_rewrite.c> around all that code in .htaccess? Sep 4, 2014 at 17:58
  • This broke the absolute links to my style sheets. What should my siteURL be set to? Sep 4, 2014 at 18:15
  • I think it's better to have an A-record for the domain pointed to the IP, and a CNAME for www.domain.com to domain.com. BUT, don't take my word for it. If you want to make sure the htaccess file works on servers that doesn't have the rewrite module installed, you could wrap it in the if, but it's not necessary. Sep 4, 2014 at 19:03
  • I forgot to say, but this rule rewrites everything except the files that are excluded (the two rewrite conditions). In my case I have all my assets in /assets and uploaded files in /uploads. If you have files in other locations, you need to add them to the rule. Sep 4, 2014 at 19:07

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.