How to invisible redirect websites in Apache. [updated]

Problem: You have 1 hosting package but 2 websites. How do you make a nice looking redirect, so that they run nicely side by side, with .htaccess?

There are 2 common hosting situations that I will explain, the first is VirtualDocumentroot what gives you access to sub domains (example: http://image.domain.com). The other one is Documentroot what will lead all the traffic to the www folder (http://www.domain.com is the same as http://domain.com).

Here you can see a demo of virtual root (sort of).

VIRTUALDOCUMENTROOT.

When you use VirtualDocumentroot, you have a root folder where you can place sub folders who represent sub domains. In the figure “VirtualDocumentroot” you can see an example of a basic hosting package using VirtualDocumentroot. There is an www-folder who represents http://www.domain.com and you have an image folder who represents http://image.domain.com.

The first thing we are going to do, is to create a .htaccess file that will move all the traffic without a sub domain to the www-folder (http://domain.com becomes http://www.domain.com). Copy and modify the following code in a standard text-editor like notepad or gedit:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^WebsiteNameA.com$ [NC]
RewriteRule ^(.*)$ http://www.WebsiteNameA.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^WebsiteNameB.com$ [NC]
RewriteRule ^(.*)$ http://www.WebsiteNameB.com/ [R=301,L]

Save as .htaccess (be careful when using windows not to add an extension) and upload it to your root folder (/) This .htaccess-file still makes it possible to use sub domains like http://image.domain.com, keep in mind that in this example it doesn’t mather witch domain name you use. Both image.WebsiteNameA.com and image.WebsiteNameB.com will work for this example.

The next thing we will do is separate both domains. For this example I made 2 folders called websitea and websiteb in the www-folder. The names you use aren’t important, you could easily use ‘cake’ and ‘beer’ as long you change the names in your second .htaccess file. What brings us to .htaccess file number 2. Again, copy and modify the following code in a standard text-editor like notepad or gedit, the /websitea is the folder name of the corresponding domain:

RewriteCond %{HTTP_HOST} ^www.WebsiteNameA.com$
RewriteCond %{REQUEST_URI} !/websitea
RewriteRule ^(.*)$ /websitea/$1

RewriteCond %{HTTP_HOST} ^www.WebsiteNameB.com$
RewriteCond %{REQUEST_URI} !/websiteb
RewriteRule ^(.*)$ /websiteb/$1

And again you need to save the file as .htaccess but this time you upload the file in the www-folder! See the figure “End result VirtualDocumentroot” to get a clue.

Let me explain what this file does, whenever people visit website A or B, the server will get the files out the corresponding folder. In this case the server will get all the files for http://www.WebsiteNameA.com out folder /www/websitea and for http://www.WebsiteNameB.com out folder /www/websiteb. The cool thing about this is that the user won’t see it happen, unlike the redirect of http://WebsiteNameA.com to http://www.WebsiteNameA.com, you also don’t need to worry about complex url’s like http://www.WebsiteNameA.com/index.php?id=12&other=test or http://www.WebsiteNameA.com/image/file.jpeg. If all the corresponding files are in the right folder, it will work.

One thing though, when you use full paths in your website’s you need to keep in mind that it’s now /www/websitea (or /www/websiteb), but normally this won’t be a problem in modern cms or blog engines like wordpress.

DOCUMENTROOT.

DocumentRoot is almost the same like VirtualDocumentroot, with the exception that your root folder is your www-folder and you don’t have sub domains. The good news is that there is less work to invisible redirect website.

Lets get started, the first thing we need to do is to split or 2 website into 2 folder. All files from website A will go in folder websitea and all files of website B will go in folder websiteb (see image ‘DocumentRoot’ for an example). The names of the folders aren’t important as long you change them in the .htacces file, what brings us to the next part: the .htaccess file. Copy and modify the following code in a standard text-editor like notepad or gedit:

RewriteCond %{HTTP_HOST} ^www.WebsiteNameA.com$
RewriteCond %{REQUEST_URI} !/websitea
RewriteRule ^(.*)$ /websitea/$1

RewriteCond %{HTTP_HOST} ^www.WebsiteNameB.com$
RewriteCond %{REQUEST_URI} !/websiteb
RewriteRule ^(.*)$ /websiteb/$1

After you modified the file, save as .htacces and upload it into your root folder.

Let me explain what this file does, whenever people visit website A or B, the server will get the files out the corresponding folder. In this case the server will get all the files for http://www.WebsiteNameA.com out folder /websitea and for http://www.WebsiteNameB.com out folder /websiteb. The cool thing about this is that the user won’t see it happen, you also don’t need to worry about complex url’s like http://www.WebsiteNameA.com/index.php?id=12&other=test or http://www.WebsiteNameA.com/image/file.jpeg. If all the corresponding files are in the right folder, it will work.

Good luck and if you have any questions, feel free to ask them.

One thought on “How to invisible redirect websites in Apache. [updated]”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.