Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
shows the content of
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.
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.
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:
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:
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With