将某些页面重定向到另一个域中的等效页面,否则重定向到其主页

redirect certain pages to its equivalent in another domain, otherwise redirect to its homepage

我想使用 htaccess 将某些页面重定向到另一个域中的等效页面,其他页面重定向到主页。

像那样:

只有这些页面会被重定向,否则,页面必须被重定向到新域主页

这是我的尝试:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>

但不知道如何排除其他页面。

这里有什么帮助吗?

提前致谢!

我不确定你说的

是什么意思

But don't know how to exclude other pages

但是当您为它们创建特定规则时,您已经排除了这些页面。这应该工作。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]

#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>

让我知道这对您有何帮助。如果您需要在尝试这些新规则之前清除缓存。

提到了另一个答案here, thank to anubhava

使用redirect而不需要使用Rewrite

RedirectMatch 301 ^/.+ http://www.newdomain.com/

所以,答案也可以是:

#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page

#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com