【问题标题】:Redirect an entire directory on a WP site to a new domain, with a few subdirectory exceptions将 WP 站点上的整个目录重定向到新域,但有一些子目录例外
【发布时间】:2013-09-12 03:23:01
【问题描述】:

我正在尝试将 WP 站点上的整个目录重定向到新域上的目录,新域中存在一些子目录例外。

例如

Redirect /2012/ to newdomain.com/blog
Redirect /2012/subdirectory to newdomain.com/2012/subdirectory

这是我所拥有的不起作用的:

#blanket redirect with exceptions
RewriteCond %{REQUEST_URI} !^/2012/$
RewriteCond %{REQUEST_URI}
!^/2012/(\/2012\/subdirectory1\/|\/2012\/subdirectory2\/|\/2012\/subdirectory1\/)
RewriteRule ^/?2012/(.+)$ http://www.newdomain.com/blog/ [L,R=301]

#redirecting the exceptions
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$
RewriteRule ^\/2012\/subdirectory1\/$
"http\:\/\/www\.newdomain\.com\/2012\/subdirectory1\/" [R=301,L]

谢谢!

【问题讨论】:

    标签: php wordpress .htaccess redirect mod-rewrite


    【解决方案1】:

    我想你只是想要一些简单的东西:

    RewriteRule ^2012/$ http://www.newdomain.com/blog/ [L,R=301]
    RewriteRule ^2012/subdirectory/?$ http://www.newdomain.com/blog/subdirectory/ [L,R=301]
    

    或许:

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

    这些规则需要您的 htaccess 文件中的任何 wordpress 规则之前。

    【讨论】:

    • 感谢您的回复!即使使用第二条规则,这仍然会将所有 2012/子目录重定向到 newdomain.com/blog/。还有其他想法吗?
    • @user2763413 第一条规则only 将请求重定向到/2012/ 或重定向到/2012/subdirectory/。没有别的,甚至没有/2012/file.txt
    • 我确实使用了您提供的第二条规则:RewriteRule ^2012/(.*)$ http://www.newdomain.com/blog/$1 [L,R=301] 它没有正确重定向“异常”。它们仍然像所有其他 /2012/子目录一样被重定向。这有意义吗?
    • @user2763413 第二条规则重定向所有内容,前两条没有
    • 如果我没有清楚地解释自己,我深表歉意。当我希望它重定向到 http://www.newdomain.com/2012/subdirectory1 时,这会将 /2012/subdirectory1 重定向到 http://www.newdomain.com/blog。我只希望少数子目录发生这种情况,其余的被重定向到http://www.newdomain.com/blog
    猜你喜欢
    • 2014-08-20
    • 2011-03-27
    • 1970-01-01
    • 2012-12-09
    • 2022-01-17
    • 1970-01-01
    • 2015-01-09
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多