Home > other >  How to use .htaccess file to get the subdomain?
How to use .htaccess file to get the subdomain?

Time:04-30

I want to redirect https://aaa.subdomain.example.com/path to https://subdomain.example.com/aaa/path
But how to do that with apache .htaccess file? I tried this, however, it didn't work :(

RewriteCond %{HTTP_HOST} ^([a-z0-9] )\.subdomain\.example\.com [NC]
RewriteRule .* https://subdomain.example.com/%1/${REQUEST_URI}

CodePudding user response:

Important: the following instructions are for information purposes only. You may need to change the code so that it works in your situation.

Using your FTP software or our FTP Manager, add and adapt the following code in the .htaccess file located in the root of your Website:

RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/ -d
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/1/?
RewriteRule "^(.*)$" "%{DOCUMENT_ROOT}/%1/$1" [L]

Explanations for the third line: [https://stackoverflow.com/a/15981056][1]

In the first line replace:

domain with your domain name com with your domain name extension (ch, fr, etc.)

  • Related