huy tran tan
huy tran tan

Reputation: 23

Get subdomain in .htaccess

I have the following .htaccess content:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_HOST} ^(.*)\.*(.+) 

RewriteRule ^((?s).*)$ index.php?_uri=/$1&_subdomain=%1&_host=%3 [QSA,L]

I want to get subdomain of the url(if it exists), but when I add a quantifier to \., the result of _subdomain is example.localhost instead of example. The full url is example.localhost. Anyone can explain to me?

Upvotes: 1

Views: 41

Answers (1)

Cray
Cray

Reputation: 2850

Use ^(.*)\.(.+)

Your current regex is making the . optional, while it should be required exactly once. Since \.* matches 0 or more times, therefore the greedy matching of ^(.*) is capturing everything up to the last character that is matched by (.+)

Upvotes: 1

Related Questions