Match all subdomains except one in mod_rewrite

Ok, so I do mod_rewrite, and I'm in a situation where I need to map any subdomain except one. He begins to look like this.

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]

Thus, to combine and fix one subdomain, there are no periods. But let me say that I do not want to match a subdomain named "dog". I tried to do this with a negative look.

RewriteCond %{HTTP_HOST} ^((?!dog)[^\.]+)\.example\.com$ [NC]

This works, for the most part. dog.example.com no longer matches, which is good. However doggies.example.com is also no longer consistent. This is not good.

I was able to fix this using a negative lookahead in combination with a negative look like this.

RewriteCond %{HTTP_HOST} ^((?!dog)[^\.]+(?<!dog))\.example\.com$ [NC]

It works. As far as I can tell, it works just fine. The fact is, I can’t believe that this is the best way to reach this match. Look and look? Indeed? What is the “right” way to achieve an equivalent expression?

+3
source share
2 answers

If you want to use a negative lookup, you need to bind the expression to a point:

RewriteCond %{HTTP_HOST} ^((?!dog\.)[^.]+)\.example\.com$ [NC]

Otherwise, anything that starts with dogwill not be allowed.

The same applies to negative lookbehind:

RewriteCond %{HTTP_HOST} ^([^\.]+(?<!^dog))\.example\.com$ [NC]

Attaching it to the beginning of a line with the help ^ensures that everything that ends with dog, as catdog, will be resolved.

, , Vinko Vrsalovic, , :

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteCond %1 !^dog$
+8

RewriteCond, :

RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dog\. [NC]
+1

Source: https://habr.com/ru/post/1715563/


All Articles