301 重定向到具有某些特定 URL 的新域

301 Redirect to new domain with some specific URLs

我看到了类似的主题,但找不到我的问题的实际答案。

我正在将我的旧网站移至新网站,一些 URL 网站正在发生变化。

我想对新域进行通用 301 重定向(因为大多数路径相同),同时单独重定向一些 URL。

这是我旧网站 .htaccess 上的内容:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www\.old\.com$
  RewriteRule (.*)$ https://new.com/ [R=301,L]

  Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>

但是 301 重定向到:https://new.com/custom/url instead of https://new.com/my-custom-url

我的一些 URL 也有 URL 个我想重定向的参数,例如:

Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"

似乎效果不佳。

非常感谢您的帮助。

But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url

这是因为您的特定重定向规则出现在通用重定向规则之后。此外,您将 mod_rewrite 规则与 mod_alias 规则混合使用,并且这些规则在不同时间被调用。

像这样:

RewriteEngine On

# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]

# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]

# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]

# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]

# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]