SSH Screen Hero

Changing your website’s root directory using .htaccess

If you host a website on one of those shared web hosts with cPanel installed, such as those offered by GoDaddy, Bluehost or Exabytes, here’s a snippet of .htaccess code that can be useful for you.

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(.*)$ /your-subdirectory/$1 [L]

It is meant to be added to the .htaccess file in your root directory, and allows you to route all queries to a sub-directory of your choice, which means it can be used to change your website’s root directory.

You’ll want to replace the highlighted domain.com in the snippet with your own domain, and your-subdirectory with your sub-directory name in your root folder.

BIG FAT NOTE: Make a backup of your .htaccess before you modify it. If it has errors, your site will most likely break with a 500 Internal Server Error on every page.


Article continues after the advertisement:


The practical application

Although .htaccess isn’t the best place to define your web root, this snippet definitely has its use. For most people, being on a shared web host means that you do not have control over where your website’s root directory is. It will simply be set for you at /home/%yourusername%/public_html (or /home/%yourusername%/www, in some cases). This in itself is actually not a problem, because you can just build your website in the folder they assigned you.

The problem only comes in if you have subdomains, because you can only house their root directories in your website’s root directory (i.e. public_html). This means that your subdomain root directories are accessible from your main website:

  • domain.com is housed in /home/%yourusername%/public_html
  • sub.domain.com is housed in /home/%yourusername%/public_html/sub
  • domain.com/sub/ will load the same content as sub.domain.com.

If we put our main website in a directory under public_html and use this .htaccess snippet to redirect the root directory to it, we will get a much nicer set-up:

  • domain.com is housed in /home/%yourusername%/public_html/main
  • sub.domain.com is housed in /home/%yourusername%/public_html/sub

Personally, I prefer to name my root directories after the domains they serve, so that they are easily identifiable, so I would go with the following directory names:

  • /home/%username%/public_html/domain.com
  • /home/%username%/public_html/sub.domain.com

Wrapping up

Personally, I’ve used this .htaccess snippet a couple of times when replacing some clients’ older websites with their new ones, so that there was no downtime during the replacement. They’d always have their old sites installed in public_html, so it was just a matter of creating a new directory to put the new site in, and using the .htaccess snippet.

Note that if you have other mod_rewrite rules in your .htaccess, you’ll want to put this snippet above all of them, since this redirect is supposed to override all of them.


Article continues after the advertisement:


Leave a Reply

Your email address will not be published.