将多个RewriteRule转换为一个RewriteCond [英] Multiple RewriteRule to one RewriteCond

查看:59
本文介绍了将多个RewriteRule转换为一个RewriteCond的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从侧面看,我有大约40万个子域.鉴于上述情况,某些调用还可以在子域级别进行操作,例如

On the side I have about 400,000 subdomains. in view of the above, some calls also operate subdomain level, e.g..

subdomain1.example.com/some_action/ 

仅应在域中执行的此类操作就有27个.

Such actions that should be performed only from the domain have 27.

Htaccess文件,我创建了一条规则:

Htaccess file I created a rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteRule ^some_action1/?$ index.php?some_action1 [L] 

如果我添加此行

RewriteRule ^some_action2/?$ index.php?some_action2 [L]

不能以与some_action3等相同的方式工作.

not work in the same way as for some_action3, etc.

我分别为每个动作添加了

I have added for each action separately

RewriteCond %{HTTP_HOST} ^(www.)?example.com

您能以某种方式跳过协调吗?

Can you somehow skip to harmonize?

推荐答案

每个RewriteCond条件仅适用于紧随其后的RewriteRule.这意味着如果您有一堆规则,则必须重复条件.如果您真的由于某种原因不想这样做,则可以在所有规则的开始处使用取反.这可能不是一个好的解决方案,因为它可能会影响您将来对规则进行更改的方式:

Each RewriteCond condition only applies to the immediately following RewriteRule. That means if you have a bunch of rules, you have to duplicate the conditions. If you really don't want to do that for some reason, you could use a negate at the very beginning of all your rules. This may or may not be a good solution since it could affect how you make future changes to the rules:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www.)?example.com
RewriteRule ^ - [L]

RewriteRule ^some_action1/?$ index.php?some_action1 [L] 
RewriteRule ^some_action2/?$ index.php?some_action2 [L] 
RewriteRule ^some_action3/?$ index.php?some_action3 [L] 

etc...

因此,第一个规则将检查主机是否为example.com的否定项,并跳过所有内容.然后,您可以添加所有规则,而不必担心这种情况.

So the first rule checks for the negative of the host being example.com, and skips everything. Then you can add all your rules without having to worry about that condition.

但是,如果您的"some_action"总是要作为GET参数的一部分,则可以只使用一条规则:

However, if your "some_action" is always going to be part of the GET parameters, you can maybe just use a single rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [L] 

这篇关于将多个RewriteRule转换为一个RewriteCond的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆