多个域的htaccess多重重写规则

时间:2020-02-14 11:19:42

标签: .htaccess redirect url-rewriting ids

我有多个网址,并希望将旧网址重定向到新网址。

下面的代码可以正常工作,例如htaccess捕获www.domain-a.com/index.php?id=41并将其以301状态代码重定向到路径为www.domain-a.com/our-conditions的同一域:

#301 redirect for domain A on a special id.
    RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
    RewriteCond %{REQUEST_URI} ^\/index\.php$
    RewriteCond %{QUERY_STRING} ^id=41(.*)$
    RewriteRule .* https://%{HTTP_HOST}/our-conditions? [R=301,L]

#410 on for domain A on a special id.
    RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
    RewriteCond %{REQUEST_URI} ^\/index\.php$
    RewriteCond %{QUERY_STRING} ^id=52(.*)$
    RewriteRule .* - [R=410,L]

#301 redirect for domain B on a special id.
    RewriteCond %{HTTP_HOST} ^www\.domain-b\.com$
    RewriteCond %{REQUEST_URI} ^\/index\.php$
    RewriteCond %{QUERY_STRING} ^id=221(.*)$
    RewriteRule .* https://%{HTTP_HOST}/press? [R=301,L]

正如您在上面看到的,仅需几个规则即可获得大量代码。 现在我认为可以做得更好,因此尝试了一些不同的方法。

我的想法是为一个域具有多个RewriteRules。通常,效果很好,就像这里的示例一样:

RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
RewriteRule ^press/material/(/?)$ https://%{HTTP_HOST}/press [R=301,L]
RewriteRule ^about/(/?)$ https://www.domain-b.de/impress [R=301,L]

现在我想出了这个试用版:

#Multiple rules for domain A on a special id.
    RewriteCond %{HTTP_HOST} ^www\.domain-a\.com$
    RewriteCond %{REQUEST_URI} ^\/index\.php$
    RewriteRule ^index.php?id=41(/?)$ https://%{HTTP_HOST}/our-conditions? [R=301,L]
    RewriteRule ^index.php?id=52(/?)$  - [R=410,L]

#Multiple rules for domain B on a special id.
    RewriteCond %{HTTP_HOST} ^www\.domain-b\.com$
    RewriteCond %{REQUEST_URI} ^\/index\.php$
    RewriteRule ^index.php?id=221(/?)$ https://%{HTTP_HOST}/press? [R=301,L]

这里的问题是我的条件不适用。例如index.php?id=41即将通过htaccess,而我的应用程序确实显示404(未找到)。

您能帮我解决这个问题吗?

0 个答案:

没有答案