htaccess rewrite to wildcard subdomain explained

Viewed 34

dear community. Today i made this .htaccess rewrite rules ( found some part on the internet and added some by myself )

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php

When i request some.site.ru - site.ru/some.php content triggered. It's fine. And when i request some.site.ru/readme.txt i see site.ru/readme.txt content. And i dont quite understand why its working fine. I thought RewriteRule ^ %1.php makes all requests to go through *.php files. Am i wrong?

Additionally i cant understand RewriteRule ^ this part. Is it the same as RewriteRule ^(.*)$ ?

1 Answers

This is your rule:

RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php

Due to presence of faulty pattern in front of RewriteCond %{HTTP_HOST} it is a DO NOTHING rule, in other words this rule will never execute because of presence of a / in 2nd condition which can never be true because %{HTTP_HOST} matches only the host name part of a web request. So %{HTTP_HOST} can only match www.example.ru or example.ru.

So it is obvious that /readme.txt URL is working fine without any rewrite.

Related