More on redirecting with Mod_Rewrite

So it’s all well and good to redirect from one domain to another but how about redirecting to a directory?

I use shared hosting and host 3-4 domains on the same hosting account. To be nice and tidy I like to keep each site in a separate sub folder. There are a few ways I could do this obviously. I could use PHP and do something like the following:

$SiteNameURL = $_SERVER['HTTP_HOST'];
switch (strtolower($SiteNameURL)) {
case "domain.com": //MUST BE LOWER CASE
            header("Location: http://" . $_SERVER['HTTP_HOST'] . "/folder/");
            break;
case "www.domain.com": //MUST BE LOWER CASE
            header("Location: http://" . $_SERVER['HTTP_HOST'] . "/folder/");
            break;
}

But that is rather ugly and it would be nice to keep all the redirecting in one place.
This is what I came up with:

#Turns the rewrite engine on.
RewriteEngine on

#Fix missing trailing slash character on folders.
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]

#www.domain.com and domain.com will map to the folder {root}/folder1/
RewriteCond %{HTTP:Host} ^(?:www\.)?domain\.com$
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteRule ^(.*) folder1/$1 [NC,L,NS]

There is a little bit of regex involved but if you step through line by line it’s really fairly simple. Of course the last 4 lines are what matters for me. I added them to my “.htaccess” file from my first post on Mod_rewrite.

Redirecting from one domain to another domain with Mod_Rewrite

Lets get started with some of the things I’m doing while I setup this site and a few others I’m working on. First thing I needed to do was redirect a few domains elsewhere as this shared web hosting is used for some domains I look after for someone else. I can’t be bothered getting the DNS changed so to so I’m going to use mod_rewrite to redirect to the domain that is in use.

So here is what I found.

So if you need to change an existing website to another domain name your web server is using Apache HTTP Server then you can use the mod_rewrite module to do the task. I used something along the lines of the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^OldDomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.OldDomain.com$
RewriteRule ^(.*)$ http://www.NewDomain.com/$1 [R=301,L]

Add this to the existing “.htaccess” file or add a new“.htaccess” file in your OldDomain‘s web root folder(example: /public_html/).

Done! Now all visitors to OldDomain.com(regardless the path) will auto redirected to NewDomain.com(with appending path).

For those of you who don’t want to fiddle with the “.htaccess” file you could just use PHP as follows:

$SiteNameURL = $_SERVER['HTTP_HOST'];

switch (strtolower($SiteNameURL)) {

case "domain.com": //MUST BE LOWER CASE
            include 'page1.php';
            break;
case "www.domain.com": //MUST BE LOWER CASE
            include 'page1.php';
            break;
case "yourotherdomain.com": //MUST BE LOWER CASE
            include 'page2.php';
            break;
case "www.yourotherdomain.com": //MUST BE LOWER CASE
            include 'page2.php';
            break;
default:
            include 'other.php';
}