How to Correctly Point your Domain Name to a Subdirectory.

It is pretty often in web design and site administration  that we are looking for more ways to secure your site against attack from hackers or malicious software.

One of the simplest and often overlooked is to place the software you are using in a subdirectory.

But how do you point your domain name example.com to your subdomain with out it looking like example.com/subdomain/ ?

The example.com/subdomain/ is usually what happens when most folks use a simple re-direct from their control panel thinking that this is the best way to accomplish the task or perhaps not knowing there is another way. But there is, to do this you are going to want to edit the .htaccess file. The .htaccess file is a very special file that should reside in each directory, it tells us the permissions of the directory, and what should be done with the contents of the directory.  For the purposed of this article I am going to provide the code for a simple redirect to subdomain. This redirect can be more complicated based upon which software you are using. So be sure to backup your .htaccess file before you get started.

After backing up we are going to want to download the .htaccess file to edit it. It CAN be done live via file manager or SSH but I feel it is best to do these things via SFTP/FTP so that you have a back up and easy version control.

Once you have downloaded you  .htaccess file we can save a backup of the backup, just to be sure.

Now open the file with your text editor. It is important to use the most basic text editor possible, notepad.exe is great! If you are on a mac you can use text edit. Other programs like Word or even Open office can toss extra code into the .htaccess which can lead to breaking your website.

When working with the .htaccess file you can use the (#) symbol to comment out things, so you can leave yourself notes about your changes. You can copy the full code block below to your .htaccess file after copying follow the instructions.

# How to file
# .htaccess main domain to subdirectory redirect
# Keep this line as a reminder.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]

Using the code above SHOULD work for you to point to php based software such as WHMCS or infinite WP etc. Visitors to your Web site will not be able to tell that your main domain is using a subdirectory, they will still see the Web site address as http://www.example.com/index.html

Some software may require special rules or actions. You may also need to modify the $base_url, $live_site or other configuration settings to finish the process. This is another great reason you should make backups during the process and you should be patient, if something is not working start over with your most recent working backup.

I hope this helps you out as much as it has helped me over the years.

 

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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