How to redirect main domain to subfolder

If you aren’t new to hosting, you may have noticed that serving content for your main domain really leaves your public directory quiet messy and it’s hard to reach other files especially if you are hosting multiple domains on a single host.

The only way to clean up this messy structure is by placing the main domain files in a sub-folder thereby providing a clean public directory. It’s easy to put all the files in a sub-folder and redirect your domain to that folder but what if you do not want that directory to appear in the url? Well you may want users to visit urls in a clean way like http://website.com/pagename and not something like http://website.com/subfoldername/pagename.

Well there is  way to clean your urls by editing the .htaccess file in the public directory. I’m assuming the public directory is clean or just has a sub-folder which you to redirect the domain. Begin by editing the .htaccess file as follows.

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

From the code about, replace website.com with your domain and subfolder with the sub-folder to redirect your website to.

Leave a Reply