2

I am using codeigniter framework i need to force to remove www from the url so I am using this code

RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]

This code is forcing removal of www. but the problem is when a user access a link with www

eg:www.mydomain.com/framework/article/sometestarticle368/

It is redirecting to

www.mydomain.com/framework/

How can i fix this ?

anubhava
  • 761,203
  • 64
  • 569
  • 643
cloudacdev
  • 91
  • 3
  • 12

2 Answers2

3

Change the order of your rules:

RewriteEngine on

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]

Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your replay this code is not working with ajax calls with www to this domain. how can i fix this? – cloudacdev Dec 16 '13 at 06:39
  • Apache doesn't know the difference between a Ajax request and regular web request. What exactly is not working in your AJAX call, can you check from Firebug and let me know? Also let me know what is full URL of your AJAX call and does it do any POST operation? – anubhava Dec 16 '13 at 07:09
0

Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
  • Thanks
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27