Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewrite subdomain to directory

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

Example:

  • http://sub.domain.com/

shows the content of

  • http://domain.com/subdomains/sub/
like image 210
Erik Djupvik Avatar asked May 17 '12 19:05

Erik Djupvik


People also ask

How to rewrite subdomain to a subfolder in htaccess?

You can use the following rule in htaccess to rewrite subdomain to a subfolder : Line by line Explaination : The line above tells the server to turn on the engine for rewriting urls. This line is a condition for the RewriteRule where We match against the http Host using regex pattern.

How to redirect a page to another page using htaccess?

Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them When you need to switch a website from an old domain to a new domain, you need to redirect all your page URLs, this is when htaccess is your friend.

How do I redirect my Old domain to a new domain?

In order to redirect your old domain to a new domain name, you should add the following rewrite rule to your .htaccess file: In order to redirect your site from a non-www to www URL, you should add the following rewrite rule to your .htaccess file:

How to redirect one directory or subfolder to another one?

In order to redirect one directory or subfolder to a different one, you should add the following rewrite rule to your .htaccess file: In order to redirect blog.oldsite.com to www.newsite.com/blog/ you should add the following rewrite rule to your .htaccess file:


1 Answers

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] 
like image 188
Ansari Avatar answered Sep 27 '22 19:09

Ansari