kdk
kdk

Reputation: 65

Multiple RewriteRule to one RewriteCond

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/ 

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] 

If i add this line

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

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?

Upvotes: 6

Views: 7012

Answers (1)

Jon Lin
Jon Lin

Reputation: 143966

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...

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.

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] 

Upvotes: 6

Related Questions