Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rewriting www to non-www and index.php CI

I am using CodeIgnter, as for a result all my links are like base.com/index.php/home/index or www.base.com/index.php/home/index. I would like to display them only as base.com/home/index if possible.I have tried looking over the internet,got the rewrite in htacces from both of them as:

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 toghether they don't do what i need them to do. Anyone knowing the solution?Thanks.

like image 425
Radu Vlad Avatar asked Mar 22 '23 05:03

Radu Vlad


1 Answers

For CI, you want something like your first set of rules, but without the redirect. But the redirect needs to happen before the routing happes, 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]  
like image 167
Jon Lin Avatar answered Apr 09 '23 00:04

Jon Lin