CodeIgniter Forums

Full Version: Redirecting www to non-www
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]alexbet[/eluser]
Hi All,

I did a CI aplication for a client which works fine. But, he also requested that I tweak the htaccess file for the www.domain.com to redirect to domain.com (without the "www" part). I tried many different things, but none would work with the CI application..

Does anyone know how to do this and still keep the CI application working properly? Any help is greatly appreciated. Thanks!

Alex

El Forum

[eluser]CroNiX[/eluser]
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^yoursite.com$
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]

Make sure to change you base_url in your config.

El Forum

[eluser]alexbet[/eluser]
Hey CroNiX,

This works great, except for one small thing. When I type in the domain as www.domain.com and when I am redirected to the non-www domain, the URL in the address bar becomes the following:

http://domain.com/index.php/

Is there any way to take avoid having the "index.php" part in the URL when you are redirected? Here is what I have now in the htaccess file:

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

Thanks SO much! Smile
Alex

El Forum

[eluser]CroNiX[/eluser]
try putting the non-www rules at the top above the index rules.

El Forum

[eluser]alexbet[/eluser]
AWESOME!!! Works just fine!

Thank you VERY MUCH CroNiX!!! Smile