4

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?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
kdk
  • 45
  • 1
  • 4
  • 3
    **I have about 400,000 subdomains:** Are you serious? – anubhava Aug 27 '14 at 06:44
  • It is a social network, each user profile is treated as a subdomain, but simply generated from htaccess. RewriteCond %{HTTP_HOST} !=www.example.com RewriteCond %{HTTP_HOST} ^(www\.)?(.+).example.com$ RewriteRule ^/?$ index.php?action=example&mode=user_profil&login=%2 [L] – kdk Aug 27 '14 at 07:11
  • there are 4 ways of doing this, listed here http://stackoverflow.com/a/24276207/5064633 – super1ha1 Jan 20 '17 at 03:08

1 Answers1

6

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] 
Jon Lin
  • 142,182
  • 29
  • 220
  • 220