Multiple RewriteRule to one RewriteCond

kdk*_*kdk 4 apache .htaccess mod-rewrite

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/ 
Run Code Online (Sandbox Code Playgroud)

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

Htaccess file I created a rule:

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

If i add this line

RewriteRule ^some_action2/?$ index.php?some_action2 [L]
Run Code Online (Sandbox Code Playgroud)

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

I have added for each action separately

RewriteCond %{HTTP_HOST} ^(www.)?example.com
Run Code Online (Sandbox Code Playgroud)

Can you somehow skip to harmonize?

Jon*_*Lin 5

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

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...
Run Code Online (Sandbox Code Playgroud)

因此,第一条规则检查主机是否为 example.com,并跳过所有内容。然后您可以添加所有规则而不必担心该条件。

但是,如果您的“some_action”始终是 GET 参数的一部分,则您可以只使用一条规则:

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