首页 > 解决方案 > 如何将所有 URL 重定向到不同的域子目录无掩码

问题描述

.htaccess 规则

Redirect 301 "/page-1/" "https://www.newdomain.com/page-1/"
Redirect 301 "/anotherpage/run/" "https://www.newdomain.com/anotherpage/run/"
Redirect 301 "/" "https://www.newdomain.com/subdirectory/"

当前结果

前两个特定页面的规则运行良好。问题在于捕获所有将在目标 url 上进行 URI 的内容。只需要指定 URI 规则以外的任何内容到 301 到新域子目录。

例如。

https://olddomain.com/page-5/ to https://www.newdomain.com/subdirectory/

预期成绩

https://olddomain.com/page-1/ redirects to https://www.newdomain.com/page-1/
https://olddomain.com/anotherpage/run/ redirects to https://www.newdomain.com/anotherpage/run/
https://olddomain.com/anypage/fun/ redirects to https://www.newdomain.com/subdirectory/

我已经尝试了几个没有运气的重写规则。它携带到新域的完整 URI。

RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/$1 [R=301,L]

标签: apache.htaccessmod-rewriteurl-rewriting

解决方案


在您的 olddomain.com .htaccess 文件顶部检查此规则

<IfModule mod_rewrite.c>
    RewriteEngine On

    # custom rewrites
    RewriteRule ^page-1/$ https://www.newdomain.com/page-1/ [R=301,L]
    RewriteRule ^anotherpage/run/$ https://www.newdomain.com/anotherpage/run/ [R=301,L]

    # rewrite all not custom matched requests to https://www.newdomain.com/subdirectory/
    RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/ [R=301,L]

</IfModule>

推荐阅读