.htaccess rewrite subdomain to directory

Can I use .htaccess to overwrite a subdomain in a directory?

Example:

shows contents

+78
url-rewriting apache subdomain dns .htaccess
May 17 '12 at 19:39
source share
8 answers

Try adding this to your .htaccess file:

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

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

 RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA] 
+100
May 17 '12 at 19:56
source share

I am not an expert on mod_rewrite, I often struggle with this, but I did it on one of my sites, this may require other flags, etc. depending on your circumstances. I use 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 should be followed subsequently so that they do not interfere with the rewriting of your subdomain.

+48
Dec 04 '13 at 22:54
source share

In htaccess, you can use the following rule to overwrite a subdomain in a subfolder:

 RewriteEngine On #If the host is "sub.domain.com" RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC] #Then rewrite any request to /folder RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L] 

Line by line Explanation:

  RewriteEngine on 

In the above line, the server is prompted to enable URL rewriting.

  RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC] 

This line is a condition for the RewriteRule, where we map the HTTP host to the regular expression pattern. The condition says that if the host is sub.domain.com , then follow the rule.

  RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L] 

The rule matches http://sub.domain.com/foo and internally redirects it to http://sub.domain.com/folder/foo .

Replace sub.domain.com with your subdomain and the folder with the name of the folder in which you want to point your subdomain.

+15
Jan 18 '16 at 15:03
source share

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

My solution (the contents of the subdomains should be in a folder named 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] 
+8
Jan 17 '14 at 10:25
source share

redirects the same folder to the subdomain: .httaccess

 RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC] RewriteRule ^(.*)$ http://domain\.com/subdomains/%1 
+4
Nov 12 '15 at 2:17
source share

Try putting this .htaccess file in a subdomain folder:

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

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

+3
Dec 30 '16 at 2:39
source share

Subdirectory directory redirection:

 RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC] RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301] 
+2
Apr 27 '16 at 6:04 on
source share

For any subdomain 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 create the folder in the same way as the subdomain name. The folder must exist as follows: domain.com/sub for sub.domain.com.

0
May 12 '16 at 10:11
source share



All Articles