htaccess删除www并重定向到访问过的网址

时间:2013-12-16 05:21:59

标签: php regex apache .htaccess codeigniter

我正在使用codeigniter框架我需要强制从网址中删除www所以我使用此代码

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]

此代码强制删除www。但问题是当用户访问带有www

的链接时
eg:www.mydomain.com/framework/article/sometestarticle368/

重定向到

www.mydomain.com/framework/

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

更改规则的顺序:

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]

否则,您的第二个规则首先运行,并在www删除规则之前将URI更改为/framework/...

答案 1 :(得分:0)

试试这个:

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