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.