【发布时间】:2017-06-08 05:39:00
【问题描述】:
我的 .htaccess 中有几个重写规则似乎相互冲突。
我正在运行一个 ExpressionEngine (CodeIgniter) 网站,该网站使用 index.php 文件解析所有 URI。出于美学原因,我已从 URI 中删除了 index.php。
我想要达到的目标:
- 使用尾部斜杠重定向 301 所有页面(example.com/bla/ =>
example.com/bla) - 从所有 URI 中删除 index.php
我现在拥有的:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
什么有效:
- 所有深层链接都被重定向到其非尾部斜杠版本 (example.com/bla/ => example.com/bla)。
- index.php 已从所有深层链接页面中删除。
什么不起作用:
- 主页 (example.com) 在 Google Chrome 中显示错误,提示“重定向太多”。
如何更新条件和规则,以便获得没有尾部斜杠和没有 index.php 的干净链接,无论它是哪个页面。
【问题讨论】:
-
如果将
RewriteRule ^(.*)/$ /$1 [L,R=301]替换为RewriteRule ^(.+)/$ /$1 [L,R=301]会怎样? -
转到 config.php 然后更改 $config['url_suffix'] = '';
标签: apache .htaccess codeigniter mod-rewrite expressionengine