.htaccess 多个域的htaccess多重重写规则

.htaccess 多个域的htaccess多重重写规则,.htaccess,redirect,url-rewriting,ids,.htaccess,Redirect,Url Rewriting,Ids,我有多个URL,希望将旧URL重定向到新URL 下面的代码确实有效,因此,例如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

我有多个URL,希望将旧URL重定向到新URL

下面的代码确实有效,因此,例如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]
正如您在上面所看到的,对于一些规则,这是大量的代码。 现在我认为可以做得更好,我尝试了一些不同的东西

我的想法是对一个域有多个重写规则。通常情况下,效果很好,如下示例所示:

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(未找到)


你能帮我让我的方法发挥作用吗?

让我们从你做得比说“不起作用”好一点开始。请阅读,然后用适当的问题描述编辑您的问题。这包括具体什么是“不起作用”的例子,以及如何(404?500?…?)@misorude:Thank man提供了诚实和良好的反馈。我阅读了操作指南并编辑了我的问题。您是对的:-)重写规则只检查请求URL的路径组件-但是您现在也在尝试匹配查询字符串内容,这是行不通的。您仍然需要使用RewriteCond来完成这一部分,就像您最初使用的那样,
RewriteCond%{QUERY_STRING}^id=41(.*)$
(这里的
(.*)
部分允许
id=41
之后的任何内容,如果这不是您想要的,那么需要将regex设置得更具体。)