Saturday, 22 November 2014

How to Host a Primary Domain from a Subdirectory (Using .htaccess)

Like some of you, you may have multiple domain names pointing to different servers, but sometimes, you may wish to have a domain name point to a specific server path (Without URL Forwarding). This post offers a solution on how to accomplish this using a web server that supports .htaccess

Preliminary - What is .htaccess (Read to understand the remainder of this post)
http://en.wikipedia.org/wiki/.htaccess

Modifying the .htaccess
The following code will need to be added to the .htaccess file in the main folder of your web server. You will need to insert the following code block and make modifications as noted in the (#) comments. You will need to change the two instances of example.com to your domain, and the three instances of subdirectory to the folder where you want your site.

Code Snippet
  1. # .htaccess main domain to subdirectory redirect
  2. # Do not change this line.
  3. RewriteEngine on
  4. # Change example.com to be your main domain.
  5. RewriteCond %{HTTP_HOST} ^(www.)?example.com$
  6. # Change 'subdirectory' to be the directory you will use for your main domain.
  7. RewriteCond %{REQUEST_URI} !^/subdirectory/
  8. # Don't change the following two lines.
  9. RewriteCond %{REQUEST_FILENAME} !-f
  10. RewriteCond %{REQUEST_FILENAME} !-d
  11. # Change 'subdirectory' to be the directory you will use for your main domain.
  12. RewriteRule ^(.*)$ /subdirectory/$1
  13. # Change example.com to be your main domain again.
  14. # Change 'subdirectory' to be the directory you will use for your main domain
  15. # followed by / then the main file for your site, index.php, index.html, etc.
  16. RewriteCond %{HTTP_HOST} ^(www.)?example.com$
  17. RewriteRule ^(/)?$ subdirectory/index.html [L]
End of Code Snippet


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/page.html.

Please note that this will not work with some website software. You may also need to modify the $base_url, $live_site or other configuration settings to finish the process.

No comments: