Rewriting www to non-www and index.php CI

I use CodeIgnter, because for the result, all my links are like base.com/index.php/home/indexor www.base.com/index.php/home/index. I would like to display them only as base.com/home/indexif possible. I tried to take a look through the Internet, got a rewriting in htacces from both of them, like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$0 [PT,L]  
RewriteCond %{HTTP_HOST} ^domain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]

and

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

put them separately, they work. But they do not do what I need. Does anyone know a solution? Thanks.

+4
source share
1 answer

For CI, you want something like your first set of rules, but without redirection. But the redirection should occur before routing starts, so try:

RewriteEngine On

# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.base\.com$ [NC]
RewriteRule ^(.*)$ http://base.com/$1 [L,R=301]

# redirect direct requests to /index.php to remove it
RewriteCond %{THE_REQUEST} \ /index\.php/?([^\?\ ]*)
RewriteRule ^ http://base.com/%1 [L,R=301]

# internally route to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [PT,L]  
+7
source

All Articles