Welcome Guest, Not a member yet? Register   Sign In
301 redirect non www to www but preserve previous rule which removes index.php
#1

[eluser]megamonk[/eluser]
i currently have this in my htaccess
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|themes|admin|js|robots\.txt)
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
Code:
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(/)?(.*) 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.

ex:

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?
#2

[eluser]Daniel Moore[/eluser]
Make sure you redirect to the "www." part first, then process the removal of the index.php afterward.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(/)?(.*) http://www.mysite.com/$1 [R=301]  
RewriteCond $1 !^(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.
#3

[eluser]tison[/eluser]
Guys,

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):

Code:
RewriteEngine On
RewriteBase /

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

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




Theme © iAndrew 2016 - Forum software by © MyBB