【问题标题】:how to do a 301 redirect for all urls in a subdirectory so that they redirect to the particular page but in the root如何对子目录中的所有 url 进行 301 重定向,以便它们重定向到特定页面但在根目录中
【发布时间】:2013-06-19 21:11:23
【问题描述】:

如何进行从 domain.com/subdirectory/url.htm 到 domain.com/url.htm 的 301 重定向,以便重定向之前在子目录中但现在在根目录中的所有 url。我有这个

重定向匹配 301 ^/subdirectory/(.*)$ http://www.domain.com/

但它会将子目录中的所有页面重定向到主页,我希望它重定向到匹配的 url。

例如重定向 domain.com/subdirectory/blue.htm 到 domain.com/blue.htm

domain.com/subdirectory/yellow.htm 到 domain.com/yellow.htm

domain.com/subdirectory/red.htm 到 domain.com/red.htm

等等。

【问题讨论】:

    标签: redirect


    【解决方案1】:

    使用mod_rewrite:

    RewriteEngine on
    RewriteRule ^subdirectory/(.*)$ /$1 [R=301,L]
    

    R=301 导致 301 重定向。 L 告诉解析器忽略任何以下规则。您可以通过$1$2$3 等访问括号内匹配的部分。

    【讨论】:

    • 感谢您的帮助,页面上出现 404 错误
    • @user2502936 省略前导斜杠。它是偶然进去的。
    • 谢谢,仍然无法为我工作,它重定向到根 domain.com
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 2021-04-04
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2012-06-19
    相关资源
    最近更新 更多