htaccess subdomain

后端 未结 1 944
一向
一向 2020-11-30 06:04

Howto make this with htacess:

subdomain.domain.com -> domain.com/subdomain (no redirect on client side)
domain.com/subdomain -> subdomain.domain.com (r         


        
相关标签:
1条回答
  • 2020-11-30 06:36

    This is a wildcard based solution, so it sould work for any number of subdomains.

    This will redirect domain.com/foo/bar to foo.domain.com/bar:

    RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
    RewriteRule ([^/]+)(/.*|$) $1.domain.com/$2 [R=302]
    

    This will handle (internal rewrite) the virtual hosts:

    RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
    RewriteRule (.*) %1/$1 [L]
    

    You might consider using 301 (permanent redirect) instead of the 302.

    I strongly suggest you have a VirtualHost for your main site domain.com or www.domain.com and a separate one for handling the virtual subdomains.

    For only a certain subdomain:

    RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
    RewriteRule (subdomain)(/.*|$) $1.domain.com/$2 [R=302,L]
    
    RewriteCond %{HTTP_HOST} ^(subdomain).domain.com$ [NC]
    RewriteRule (.*) %1/$1 [L]
    
    0 讨论(0)
提交回复
热议问题