Codeigniter htaccess to remove index.php and www
.htaccess, apache, codeigniter, mod-rewrite, php
Solution
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
`RewriteEngine On` "starts" rewriting. `RewriteCond` and `RewriteRule` work as pairs. Here, we send user to non-www version first, and cleans URLs.
Problem
I'm trying to get my htaccess rewrite rules to remove the `index.php` from the url AND also redirect the `www.` requests to the non-www version. This is my htaccess which works fine with removing the index.php: ``` RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] ``` And I came across another question on how to remove the `www` part: ``` RewriteEngine On RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L] ``` But I just cant seem to get them to play nicely together! Any advice/suggestions most appreciated!