Reputation: 73
For example, I have URL subdomain.example.com
.
How can I get the subdomain and domain from the URL in .htaccess
?
I need to do something like:
RewriteRule (.*) example.com/subdomain/index.php
I figured it out for a subdomain, but still no luck for the domain.
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com
RewriteRule (.*) example.com/%1/index.php
Upvotes: 2
Views: 2073
Reputation: 45829
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com RewriteRule (.*) example.com/%1/index.php
The same principle applies for the domain (although the CondPattern you are using here appears to be invalid?). It looks like you need the domain+TLD (basically, everything after the subdomain), so try something like:
RewriteCond %{HTTP_HOST} ^(.+?)\.(.+?)\.?$
RewriteRule ![^/]+/[^/]+/index\.php$ %2/%1/index.php [L]
The (.+?)
part of the CondPattern grabs the subdomain (the ?
makes it non-greedy). The (.+?)
part grabs the domain (remaining part of the host), again this is non-greedy to avoid matching the optional trailing dot on a fully qualified domain (eg. subdomain.example.com.
).
The RewriteRule
pattern ![^/]+/[^/]+/index\.php$
prevents a rewrite loop (500 error) by rewriting only when the URL is not of the form /<something>/<something>/index.php
.
Upvotes: 2