dsewbh5588 2013-12-16 05:21
浏览 27
已采纳

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

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 ?

  • 写回答

2条回答 默认 最新

  • dorisdong0514 2013-12-16 05:50
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 CSS实现渐隐虚线框
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容