How to create multi subdomains with htaccess from folders, let's say I have my folders like this
/folder1/folder2/
and I want my address to be so folder1.folder2.dominio.com
How to create multi subdomains with htaccess from folders, let's say I have my folders like this
/folder1/folder2/
and I want my address to be so folder1.folder2.dominio.com
RewriteEngine on
RewriteCond %{HTTP_HOST} subdominio.mipagina.com [NC]
RewriteRule ^(.*)$ http://www.mipagina.com/subcarpeta/$1 [L,NC,QSA]
The first and second lines (Options and RewriteEngine) serve to enable the rewriting of urls.
The following RewriteCond line will create a condition, which means that whenever the called url matches subdomain.mipagina.com it will be fulfilled and it will execute the following rule, but simply ignore it. [NC] serves not to distinguish between uppercase and lowercase and. They serve to escape the point since it is a special feature of the rewrites.
The last RewriteRule line will pick up any content that we send against that subdomain with (. *), that is, if we enter subdomain.mipagina.com/algomas, the algomas part will be saved as variable and we will include it later. The next part is the destination link $ 1, the subfolder we want to send it to. The $ 1 is used to place the variable that we have said before, so when loading the subdomain that I mentioned it would redirect it to link , so we could enter any subfolder and file that has been written in the browser automatically. Lastly [L, NC, QSA] means:
I find it a bit complex to rewrite more than one subdomain, since you would need to replace them in the rewriteRule. by /
For a single subdomain, it would suffice:
RewriteEngine On
RewriteBase /
# No reescribimos directorios reales...
RewriteCond %{REQUEST_FILENAME} !-d
# ... ni archivos
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([a-z0-9\.]+)\.dominio\.com$ [NC]
RewriteRule ^([^/]+)/?$ %1/$2 [QSA,L]
finally, this would serve for domain of type:
subd.domain.com -> <path_base>/subd
subd1.subd.domain.com -> <path_base>/subd1.subd
I hope it serves you
greetings