【问题标题】:How to resolve conflicting mod_rewrite rules and conditions如何解决冲突的 mod_rewrite 规则和条件
【发布时间】: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


【解决方案1】:

像这样:

RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule ^(.*?)index\.php(/.*)?$ /$1$2 [R=301,NE,L]

RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]

在测试前清除浏览器缓存。

【讨论】:

  • 使用此解决方案,主页的重定向循环仍然存在。 example.com 将我重定向到 example.com/index.phpexample.com/index.php 将我重定向到 example.com。还有其他想法吗?
  • 清除浏览器缓存后试试我更新的规则。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
相关资源
最近更新 更多