将 www 重写为非 www 和索引.php CI


rewriting www to non-www and index.php CI

我正在使用CodeIgnter,因此我所有的链接都像base.com/index.php/home/indexwww.base.com/index.php/home/index。如果可能的话,我只想base.com/home/index地显示它们。我尝试在互联网上查看,从他们俩那里得到了 htacces 的重写:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$0 [PT,L]  
RewriteCond %{HTTP_HOST} ^domain'.com'$ [OR]
RewriteCond %{HTTP_HOST} ^www'.domain'.com'$
RewriteRule ^/?$ "http':'/'/domain'.com'/" [R=301,L]

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

将它们分开,它们有效。但是他们不做我需要他们做的事情。有人知道解决方案吗?谢谢。

对于 CI,您需要类似于第一组规则的东西,但没有重定向。但是重定向需要在路由启动之前发生,因此请尝试:

RewriteEngine On
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www'.base'.com$ [NC]
RewriteRule ^(.*)$ http://base.com/$1 [L,R=301]
# redirect direct requests to /index.php to remove it
RewriteCond %{THE_REQUEST} ' /index'.php/?([^'?' ]*)
RewriteRule ^ http://base.com/%1 [L,R=301]
# internally route to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [PT,L]