Home > Blockchain >  htaccess: Wildcard subdomain to subfolder of same name for all domains
htaccess: Wildcard subdomain to subfolder of same name for all domains

Time:10-02

In my htaccess, I wildcard redirect all subdomains to the subfolder of the same name. However I'm needing to add this for each domain I have. I would like to have this compacted to auto do for absolutely any domain.

I do have this for removing www. from requests:

RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]

The rule I have for wildcard subdomains to subfolders is:

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.] )\.domain\.tld$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ http://domain\.tld/%1/$1 [L,QSA]

Taking note of the way the www removal rule works, using the same behaviour using (.*) and %1 in place of the tld.. wouldn't work because that same behaviour is being used for the subdomain/subfolder.

Is there any way I can achieve this? It'll hugely cut down on my htaccess by not having this repeated for every domain.

Edit: Ok, so you can have multiple capture groups/regex, though I am confused how to get that to work.

Also found from this page this more tidy version that takes away the second condition, as well as the www part as I remove the www anyway so it doesn't matter from what I can see.

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld
RewriteRule ^(.*)$ http://domain.tld/%1/$1 [L,NC,QSA]

Edit 2: Looks like I have a winner here. Took the www removal condition and switched it up and got the logic of how you do it.

RewriteCond %{HTTP_HOST} ^([^.] )\.(.*)
RewriteRule ^(.*) http://%2/%1/$1 [R,L]

Edit 3: This works for every domain besides 1.. a cz domain. It switches the domain name around to be a subfolder of the damn extension like domain.cz to cz/domain along with any subdomain as a secondary subfolder. I'm honestly so so so confused with this.

CodePudding user response:

Ahh I see what's going on. [^.] Is matching anything before the first dot, so without a sub, it does the tld, and if you have a sub of that tld, it does the top level domain and then the sub of it so you end up with /tld/sub as such that I saw with my cz domain.

My main domains I have pointed to vhosts this rule isn't present in, and my alias domains I have pointed to this vhost where the rule is present, but as they're redirected to my main domains before this rule.. they're not caught by it.

Meanwhile my .cz domain I don't currently have going anywhere and have it pointing to the same vhost as the rule, and so it gets caught by it.

I'm sure someone would be able to come up with regex that ensures tlds get ignored, if you want to have a one vhost approach.. but that's just increasing complexity, especially also with there being different number of extensions like .co.uk and .com. And if you only have a one vhost approach such as redirect domains to subfolder sites.. then they won't get caught by that regex, as long as that is above this rule redirecting subs which you want at the bottom of the htaccess.

So it's working as intended, when I actually have my domains setup in the way I intended, which I just hadn't done for my cz one so far lol.

So this does work fine this rule.

RewriteCond %{HTTP_HOST} ^([^.] )\.(.*)
RewriteRule ^(.*) http://%2/%1/$1 [R,L]
  • Related