【问题标题】:Recurring URL when redirecting with .htaccess使用 .htaccess 重定向时重复出现的 URL
【发布时间】:2013-03-04 16:55:46
【问题描述】:

我正在尝试将规则添加到我的 .htaccess 文件中,这样如果我要更改我的网站,它将在用户尝试访问我的网站时将其重定向到“正在建设中”页面。

我正在使用:

# SITE UNDER CONSTRUCTION REDIRECT
Redirect 302 / http://redsquirrelsoftware.co.uk/construction.html

但这会将我发送到http://redsquirrelsoftware.co.uk/construction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.html,我显然会收到 404 错误。

如何停止网址上的递归尾部?

【问题讨论】:

    标签: .htaccess redirect http-status-code-302


    【解决方案1】:

    Redirect 是一个 mod_alias 指令,不能与 mod_rewrite 指令混合使用,因此为了防止循环,您应该只使用这样的 mod_rewrite 指令:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !construction\.html   [NC]
    RewriteCond %{REQUEST_URI} !folder_to_exclude    [NC]
    RewriteRule .*           /construction.html      [R=301,L]
    

    此答案假定/construction.html 页面位于同一域中。如果不是,请替换:

    RewriteRule .*             /construction.html         [R=301,L]
    

    RewriteRule .*   http://new_domain/construction.html  [R=301,L]
    

    【讨论】:

    • 像魅力一样工作谢谢!两点,在最后一行你有[R=301,L] - 我猜301是重定向代码?在哪种情况下应该将其更改为 302,因为它只是一个临时重定向?
    • 其次,我将如何从这个重定向中排除一个文件?例如,如果有一个 T&C 页面,是否可以将其从重定向中排除?
    • 是的,将其更改为R=302 或只是R 没有问题。我将使用排除更新答案。
    【解决方案2】:

    尝试使用前置条件:

    RewriteCond %{REQUEST_URI} !^/construction.html
    Redirect 302 / http://redsquirrelsoftware.co.uk/construction.html
    

    【讨论】:

    • 我尝试添加该行,我得到 - 您无权访问 /construction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction。此服务器上的 htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.htmlconstruction.html。此外,在尝试使用 ErrorDocument 处理请求时遇到 302 Found 错误。
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多