Score:0

htaccess redirect all domains to their folders

zw flag

Recently i bought another domain, and i have all of them on 1 hosting. My hosting provides me only with htaccess file where are all redirects from subdomains made. The folder structure now looks like this:

\---www
    |   .htaccess
    |
    \---domains
        +---example.com
        +---site.test.net
        \---test.example.com

but because there is lot of folders with different domains and subdomains, i would like to make it looks better, so i would like to create rule in htaccess that will redirect them into folders categorized by their root domain (example.com, test.net etc.). I tried to search it but i couldnt find any answer that will suit my problem. After week of googling i asked ChatGPT (i know, not the best source to ask) and it provided me with this code:

RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule .* - [E=DOMAIN:%2]

RewriteCond %{ENV:DOMAIN} !^$
RewriteCond %{REQUEST_URI} !^/domains/
RewriteRule ^(.*)$ /domains/%{ENV:DOMAIN}/%{ENV:DOMAIN}/$1 [L]

I see that its trying to take only the part of the www that is not in this example presented, so its still trying to find folder called "domains/test.example.com/test.example.com". Is there even any way to do something like that? Because i even searched the list of variables, that could be possibly used in htaccess and none of them included root domain.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.