Andy
Andy

Reputation: 515

htaccess automatically detect domain name

I made script that automatically create subdomain.

the htaccess working fine, but the problem is, i need to manually change domain name when install on new domain.

I want to automatically detect domain name in htaccess, is that possible?

so when adding in another server or new domain, no need to change the domain root htaccess again.

domain root htaccess is:

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][-a-z0-9]+)\.mydomain\.com(:80)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com

i try to change automatically get from %{HTTP_HOST}

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][-a-z0-9]+)\.%{HTTP_HOST}(:80)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST}

the problem is on %{HTTP_HOST}, how to get/match current domain in current url with htaccess?

maybe need to get domain name as variable.

Thank you

Upvotes: 3

Views: 2911

Answers (2)

RafaSashi
RafaSashi

Reputation: 17225

In addition to anubhava's answer if you need to capture the rest of the url after domain.com in %3 you can use the following RewriteCond

RewriteCond %{HTTP_HOST} ^(?:www\.)?((?!www\.)[^.]+\.)?([^/.]+\.[^/.]+)/?([^/]+)?$ [NC]

Upvotes: 0

anubhava
anubhava

Reputation: 786291

I believe this RewriteCond should work for you:

RewriteCond %{HTTP_HOST} ^(?:www\.)?((?!www\.)[^.]+)\.([^.]+\.[^.]+)$ [NC]

So for ex; with a URL like http://www.sub.domain.com it will capture sub in %1 variable and domain.com in %2 variable.

Upvotes: 3

Related Questions