cloudacdev
cloudacdev

Reputation: 91

htaccess remove www and redirect to accessed url

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 ?

Upvotes: 2

Views: 293

Answers (2)

Anand Solanki
Anand Solanki

Reputation: 3425

Try this:

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

Upvotes: 0

anubhava
anubhava

Reputation: 785246

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..

Upvotes: 3

Related Questions