【问题标题】:.htaccess redirection fails when slash in index.php/.htaccess 重定向在 index.php/ 中的斜杠时失败
【发布时间】:2015-12-21 12:07:58
【问题描述】:

我的 .htaccess 文件中有以下代码,

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /home
RewriteRule ^home$ / [L,R=301]

www.domainname.com/index.phpwww.domainname.com 时成功重定向。

但是当 URL 像 www.domainname.com/index.php/ 一样时,它无法重定向 (在 index.php 后带有斜线的 URL)

【问题讨论】:

  • 当您请求一个带有斜杠的 URL 时会发生什么?您收到 404 还是页面仍在加载?
  • 该页面加载,但我需要重定向它..

标签: php linux .htaccess


【解决方案1】:

通过在两条规则中使尾部斜杠可选来实现这一点:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /+index\.php [NC]
RewriteRule ^index\.php/?$ / [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /home [NC]
RewriteRule ^home/?$ / [L,NC,R=301]

【讨论】:

  • 您必须将此规则作为第一条规则放在您的 htaccess 中。
猜你喜欢
  • 2018-08-04
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多