Redirect main domain to subdirectory using .htaccess

On this post I will show you how to redirect domains to subdirectories. This is specially useful when you migrate data from the main domain to a sub-folder or sub-directory and you don’t want to lose your traffic on the main domain. Let’s see how to redirect main domain to subdirectory using .htaccess file.

How can I redirect main domain to subdirectory?

If you are looking for a way to redirect from to, this can be done using a few directives from your .htaccess file.

Try using the following code, copy and paste it inside your .htaccess file, but first make sure you replace “” with your real domain name, and the subdirectory with your real subdirectory name where you want to redirect to.

RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subdirectory/$1 
RewriteCond %{HTTP_HOST} ^(www.)?$ 
RewriteRule ^(/)?$ subdirectory/index.php [L]

That’s all. Test this redirect technique and let us know if you were able to redirect main domain to subdirectory or not. In our test enviroment using Apache 2.2 this worked fine.

Recommended reading:

About the Author: Santiago Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

Leave a Reply

Your email address will not be published. Required fields are marked *