Nick S.
Nick S.

Reputation: 353

Redirect www to non-www AND remove index.php

I have a router set up that rewrites /index.php/this/that to simply /this/that, using:

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

How can I also redirect www to non-www? I've tried numerous variations of the following, but haven't had success yet:

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

Thanks!

Upvotes: 3

Views: 721

Answers (1)

anubhava
anubhava

Reputation: 785481

Just place www removal code before other rules:

RewriteEngine On

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

RewriteRule ^$ index.php [L]

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

Upvotes: 3

Related Questions