【问题标题】:Recurring URL when trying Redirect 301尝试重定向 301 时重复出现的 URL
【发布时间】:2020-05-15 17:29:42
【问题描述】:

我正在尝试通过在末尾附加 /public 来重定向对目录的任何请求。将这一行添加到我的 .htaccess 文件中:

Redirect 301 /somedirectory/ http://www.my-site.com/somedirectory/public/home.shtml

然后尝试访问 www.my-site.com/somedirectory 会导致浏览器(桌面版 Firefox,但也尝试过桌面版 Edge 和移动版 Firefox,结果相同)显示以下内容:

页面未正确重定向 Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。此问题有时可能是由禁用或拒绝接受 cookie 引起的。

地址栏中显示的网址是这样的:

http://www.my-site.com/somedirectory/public/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtmlpublic/home.shtml

有谁知道发生了什么以及如何纠正?

【问题讨论】:

  • 嗨,有趣,不确定这是否会引起兴趣stackoverflow.com/questions/21417263/…
  • 重定向路径 prefixes 上的匹配项,并将请求的 URL 的其余部分附加到新 URL - /somedirectory/ 匹配 /somedirectory/public/home.shtml 作为前缀,因此再次重定向,并附加了多余的部分。您不能使用 Redirect 指令执行此操作,这需要 RedirectMatch(在末尾锚定模式)或 RewriteRule。
  • @CBroe - 明白了,这是有道理的。由于我仍在学习这一点,我可能会在解决重写的乐趣之前尝试 RedirectMatch!我想我需要类似于RedirectMatch 301 ^/somedirectory/(.*)$ http://www.my-site.com/somedirectory/public/$1 的东西
  • @IronMan - 感谢您的建议,我想知道尾随/前导斜杠是否与它有关

标签: .htaccess redirect


【解决方案1】:

经过进一步的实验,我现在用 mod_rewrite 解决了这个问题。我想要的行为是转发到 /public 目录,除非在请求根目录 my-site.com 时,因为我的主要登录页面位于根目录中。所以我成功地使用了这个:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?my-site\.com$ [NC]
    RewriteCond %{REQUEST_URI} /(directory\-one|directory\-two|directory\-three)/?$
    RewriteRule ^(.*)$ /%1/public/ [R,L,END]
</IfModule>

由于我目前只有 3 个目录我想要这种行为,一切都很好,但如果我添加更多目录时,我可能会更改逻辑以排除一些特定目录,而不是包含很多目录。

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 2016-12-18
    • 1970-01-01
    • 2016-03-23
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多