【问题标题】:Apache RewriteRule infinite loopApache RewriteRule 无限循环
【发布时间】:2013-09-03 19:34:13
【问题描述】:

我正在重建网站的链接结构。

对于某些部分,网站现在使用实际的 php 脚本进行响应,例如:

DOMAIN/about-us.php

我必须像这样重新映射那些类型的 URL(这只是一个例子):

DOMAIN/about-us.php --> DOMAIN/uk/about-us

由于这些页面已经编入索引,我想使用 301 重定向到新资源。

我使用了这些规则:

RewriteRule ^/about-us.php$ /uk/company/about-us   [R=301,L]
RewriteRule ^/uk/company/about-us$ about-us.php   [L]

但这会陷入无限循环。

我尝试了不同的解决方案,但没有成功(我也尝试了这个:How to stop .htaccess loop - 建议的第二种方法)。在我的环境中唯一有效的是重命名 about-us.php 并更新规则。但由于我们有很多脚本重命名所有这些并不是最佳选择。

【问题讨论】:

  • 根目录下都是php文件吗?你希望它作为 /uk/company/about-us 工作吗?
  • 嗨,Prix,大部分脚本都在根文件夹中。对于上面的示例,是的,URL 将是 /uk/company/about-us,它在文件系统中不存在

标签: apache .htaccess mod-rewrite


【解决方案1】:

试试这个:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1 [R=301,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule /?([^/]+)/?$ $1.php [L]

仅适用于根文件夹中的 php。

【讨论】:

  • 谢谢 Prix,我试过了,它成功了,但下面我会写一个更简单的步骤来做同样的事情。
  • 这是我自己提出的解决方案 RewriteCond %{ENV:REDIRECT_STATUS} !200 RewriteRule ^about-us.php$ /uk/company/about-us [R=301,L ] RewriteCond %{ENV:REDIRECT_STATUS} !200 RewriteRule ^contact-us.php$ /uk/company/contact-us [R=301,L] RewriteRule ^uk/company/contact-us$ contact-us.php [L ] RewriteRule ^uk/company/about-us$ about-us.php [L] 它似乎按预期工作。
  • @user2742557 没有看到当您必须为您拥有的每条规则手动制作时,这有多简单。 But if my solution worked for you, kindly mark it as the answer, see here how.
  • 您的解决方案适用于我描述的单一情况。虽然我的评论可能对不同的情况有用。顺便说一句,我可以接受你的回答。
猜你喜欢
  • 2013-02-08
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多