.htaccess rewrite subdomain to directory

前端 未结 8 1867
不思量自难忘°
不思量自难忘° 2020-11-22 10:26

Is it possible to use .htaccess to rewrite a sub domain to a directory?

Example:

  • http://sub.domain.com/

shows the content of

相关标签:
8条回答
  • 2020-11-22 10:58

    Try putting this in your .htaccess file:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^sub.domain.com
    RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
    

    For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
    RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
    
    0 讨论(0)
  • 2020-11-22 11:01

    For any sub domain request, use this:

    RewriteEngine on 
    RewriteCond %{HTTP_HOST} !^www\.band\.s\.co 
    RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.co 
    RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+) 
    RewriteRule ^(.*)$ /%1/$1 [L] 
    

    Just make some folder same as sub domain name you need. Folder must be exist like this: domain.com/sub for sub.domain.com.

    0 讨论(0)
  • 2020-11-22 11:03

    I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm

    My solution (the subdomains contents should be in a folder called sd_subdomain:

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTP_HOST} subdomain\.domain\.com
    RewriteCond $1 !^sd_
    RewriteRule (.*) /sd_subdomain/$1 [L]
    
    0 讨论(0)
  • 2020-11-22 11:06

    Try to putting this .htaccess file on subdomain folder:

    RewriteEngine On
    
    RewriteRule ^(.*)?$ ./subdomains/sub/$1
    

    It redirects to http://domain.com/subdomains/sub/, when you only want it to show http://sub.domain.com/

    0 讨论(0)
  • 2020-11-22 11:10

    I'm not a mod_rewrite expert, I often struggle with it, but I have done this on one of my sites, it might need other flags etc depending on your circumstances. I'm using this:

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

    Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.

    0 讨论(0)
  • 2020-11-22 11:10

    This redirects to the same folder to a subdomain:

    .httaccess

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain\.com/subdomains/%1
    
    0 讨论(0)
提交回复
热议问题