EllisLab text mark
Advanced Search
301 redirect non www to www but preserve previous rule which removes index.php
Posted: 10 January 2011 11:02 PM
Joined: 2009-10-29
11 posts

i currently have this in my htaccess

RewriteEngine on
RewriteRule ^(.*)$ /index.php/$1 [L] 

this basically strips out the index.php

now i need to redirect all non www to www url so i tried adding this

RewriteCond %{HTTP_HOST} ^mysite.com[NC]
^(/)?(.*) http://www.mysite.com/$1 [R=301,L] 

it works fine except that the index.php is added after the url whenever a non www url is typed.


mysite.com = http://www.mysite.com
mysite.com/about = http://www.mysite.com/index.php/about

i want to remove that “index.php”. any ideas?

Posted: 11 January 2011 11:28 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-05-09
234 posts

Make sure you redirect to the “www.” part first, then process the removal of the index.php afterward.

RewriteEngine On
%{HTTP_HOST} ^mysite.com[NC]
^(/)?(.*) http://www.mysite.com/$1 [R=301]  
RewriteCond $!^(index\.php|assets|images|themes|admin|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [R=301,L] 

Don’t put an L next to R=301 when forcing a redirect to “www.”, or no more rules will be processed.

Also, add the R=301 to the removal of the index.php, so that search engines will remember not to try the index.php next time.


Daniel Moore
Using .htaccess to remove index.php

Posted: 26 August 2011 11:23 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-05-23
78 posts

How do you know if it worked?

Posted: 01 May 2012 08:43 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-11-07
12 posts


This is the final code I used - this not only redirects index.php, but also redirects non-www requests to www.  (if you’re using non-www, simply remove the middle rules):

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f
^(.*)$ index.php/$1 [L]