Redirect wildcard subdomains to subdirectory, without changing URL in address bar

前端 未结 3 1153
别跟我提以往
别跟我提以往 2021-01-01 06:04

I\'ve read a lot of questions and answers about this on here but none that seem to solve my specific problem.

I want to redirect any subdomain to the subdirectory to

3条回答
  •  时光说笑
    2021-01-01 06:54

    I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:

    /
     var/
         www/
             html/
                  .htaccess
                  subdomain1.domain.com/
                  subdomain2.domain.com/
                  subdomain3.domain.com/
    

    Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
    RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
    

    It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.

    Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

提交回复
热议问题