【问题标题】:301 Redirect from /%anyfolder%/ to /Specific folder/ - Apache301 从 /%anyfolder%/ 重定向到 /Specific folder/ - Apache
【发布时间】:2020-04-27 04:39:09
【问题描述】:

我有一个网站,其中包含一堆旧的域路径。例如。

https://example.com/path1/file.html

https://example.com/path2/file.html

https://example.com/path3/file.html

https://example.com/path4/file.html

我想将所有流量从任何路径重定向到指定路径

https://example.com/my-new-path/file.html

我在我的 htaccess 文件中尝试了以下内容

RedirectMatch 301 ^.*/file.*$ /my-new-path/

但是,这会创建一个重定向循环,我明白为什么。问题是,我不知道如何解决这个问题。

我怎样才能为 .* 任何东西创建一个捕获所有重定向,除了 my-new-path

【问题讨论】:

    标签: regex .htaccess redirect http-status-code-301


    【解决方案1】:

    您可以像这样使用负前瞻正则表达式:

    RedirectMatch 301 ^/(?!my-new-path/)[\w-]+/(file\..+)$ /my-new-path/$1
    

    (?!my-new-path/) 是一个否定的先行断言,它匹配使用 [\w-]+ 的下一个模式,除了 my-new-path

    确保使用新浏览器或清除浏览器缓存来测试此规则。

    【讨论】:

    • 抱歉因为别的事情被拖走了,我会尽快测试并告诉你,我认为这已经足够了。谢谢!
    • 您好 anubhava 先生,如果我使用 RewriteCond 和 RewriteRule 方法并在那里使用重定向,您能否告诉我是否会有区别?对不起,因为我是新手,所以在这里问你,谢谢。
    • 当然你也可以使用基于mod_rewrite的规则。
    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2014-04-04
    • 2011-10-10
    • 2013-05-26
    • 1970-01-01
    • 2013-08-18
    • 2013-08-20
    • 2014-01-14
    相关资源
    最近更新 更多