如何一揽子重定向所有URL到不同的域子目录没有掩码

时间:2019-08-07 13:55:00

标签: apache .htaccess mod-rewrite url-rewriting

.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]

1 个答案:

答案 0 :(得分:1)

在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>