HTACCESS对多个重写的重新写入?
我在.htaccess中的重定向方面有麻烦。 我有几个域(example.com,example.net,example.uk)。我为每个域都有一套重写。但是由于某种原因,重定向适用于所有域。以下是我尝试过的HTACCESS规则文件。
RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com) [NC]
RewriteRule ^1example$ / [R=302,L]
RewriteRule ^2example$ /example2 [R=302,L]
RewriteRule ^3example$ / [R=302,L]
RewriteRule ^4example$ /example4 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.net|example\.net) [NC]
RewriteRule ^5example$ /sgsg [R=302,L]
RewriteRule ^6example$ /example2 [R=302,L]
RewriteRule ^7example$ / [R=302,L]
RewriteRule ^8example$ /example44 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.uk|example\.uk) [NC]
RewriteRule ^9example$ / [R=302,L]
RewriteRule ^10example$ /example12 [R=302,L]
RewriteRule ^11example$ / [R=302,L]
RewriteRule ^12example$ /example41 [R=302,L]
您是否知道如何仅针对特定域分开重写?
I have trouble with redirects in .htaccess.
I have several domains (example.com, example.net, example.uk). I have set of RewriteRules for each domain. But for some reason, the redirects works for all domains. Following is my tried htaccess rules file.
RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com) [NC]
RewriteRule ^1example$ / [R=302,L]
RewriteRule ^2example$ /example2 [R=302,L]
RewriteRule ^3example$ / [R=302,L]
RewriteRule ^4example$ /example4 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.net|example\.net) [NC]
RewriteRule ^5example$ /sgsg [R=302,L]
RewriteRule ^6example$ /example2 [R=302,L]
RewriteRule ^7example$ / [R=302,L]
RewriteRule ^8example$ /example44 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.uk|example\.uk) [NC]
RewriteRule ^9example$ / [R=302,L]
RewriteRule ^10example$ /example12 [R=302,L]
RewriteRule ^11example$ / [R=302,L]
RewriteRule ^12example$ /example41 [R=302,L]
Do you have any idea how to separate RewriteRules only for specific domain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
带有您的样品和尝试;请尝试以下内容.htaccess规则文件。根据Anubhava的评论,在每个
重写
之前添加了rewriteCond
条件。在测试URL之前,请确保清除浏览器缓存。
适用于OP的尝试的修复程序:
重写 ^(?:1 | 3)示例$/[r = 302,l,nc ]
上面文件中的第一条规则。nc
在rewriteCond
和重写
中添加。rewriteCond
中的非捕捉组来制作www。 /code> to
^(?:www \。)?示例\ .com $
With your shown samples and attempts; please try following .htaccess rules file. As per anubhava's comments have added
RewriteCond
conditions before eachRewriteRule
.Please make sure to clear your browser cache before testing your URLs.
Fixes applied to OP's attempts:
RewriteRule ^(?:1|3)example$ / [R=302,L,NC]
very first rule in above file.NC
flag in bothRewriteCond
andRewriteRule
in all of the rules.RewriteCond
to makewww.
optional eg: FROM^(www\.example\.com|example\.com)
TO^(?:www\.)?example\.com$
如前所述,
rewriteCond
(条件)指令本身仅适用于以下的第一个重写
。RewriteCond
指令构成单个规则的一部分。但是,您可以以各种方式“解决”此操作,而无需将相同的条件应用于多个规则。
例如:
或者,您可以暂时将主机名前缀到URL路径,并直接在
重写
指令中对其进行测试。例如:
As already mentioned, the
RewriteCond
(condition) directive itself applies only to the firstRewriteRule
that follows. TheRewriteCond
directive forms part of a single rule.However, you can "workaround" this in various ways, without having to apply the same condition to multiple rules.
For example:
Alternatively, you could temporarily prefix the hostname to the URL-path and test for this directly in the
RewriteRule
directive.For example: