View Full Version : Redirecting domains and PHP scripts

09-02-2004, 11:48 AM
Hi! :)

I present you with a problem I faced and its solution I found today. If anybody has a better solution, feel free to inform me. I hope my solution can help other people who may have a similar problem.

The problem: How to use more than one domain names with PHP scripts that normally work only on one domain.

I recently decided to add two more domain names to my website (http://www.wikinerds.org). I made the domains to point to specific directories within my site:

Adapedia.org -> wikinerds.org/adapedia

nerdyPC.org -> wikinerds.org/nerdypc

In these directories live two installations of MediaWiki (http://wikipedia.sourceforge.net), a PHP (http://www.php.net) script.

This script asks for a domain name when you install it. It seems unable to work with more than one domain name. So if www.adapedia.org redirects to wikinerds.org/adapedia, the script will not work and the user will get an error message.

My solution:

Step 1: I created two subdomains adapedia.wikinerds.org and nerdypc.wikinerds.org which point to the /adapedia and /nerdypc directories respectively. I re-installed the script to these subdomains. This step was not required, but I did that because I think subdomains are easier for the user to remember and may organise the script's cookies better.

Step 2: I created two directories named /fwd/ap and /fwd/npc. I placed an index.html files in each directory. The index.html file contains a META redirect which transfer the user to the desired subdomain of wikinerds.org.

Below you can see the template of the index.html file I used:

<html><head><title>Redirect Page</title>
<meta http-equiv="refresh"
<a href="http://subdomain.domain.tld/">Click here!</a>

The number 0 controls how much time the user's browser will wait until it transfers the user to the destination. It's in seconds IIRC.

Step 3: Using Westhost (http://www.westhost.com)'s Site Manager 2.0 I went to the Domain Management area and configured adapedia.org and nerdypc.org to redirect to /fwd/ap and /fwd/npc directories respectively.

Result: The user can type www.adapedia.org and he/she sees a small page containing a link. If the user's browser supports redirects, the user will be transferred to adapedia.wikinerds.org. If it doesn't, the user can click the link. The PHP script now works correctly since it only operates in the adapedia.wikinerds.org subdomain. Problem solved! :)

Possible problems: Some search engines don't like META redirects because they seem to be used a lot by spammers. Some browsers may not support META tags.

If you think this guide is useful, you may copy and alter it under the terms of CreativeCommons Attribution 2.0 (http://creativecommons.org/licenses/by/2.0/) license.

09-02-2004, 03:30 PM
Possible problems: Some search engines don't like META redirects because they seem to be used a lot by spammers. Some browsers may not support META tags.

The fix for this is to create an .htaccess file in that directory and put something like this in it . . .

redirect /index.html http://www.yourdomain.com/some/dir/index.html

Of course, change the place it's redirected to. Since you have 2 domains, you'll have to create 2.

Credit where credit is due. This information was stolen from another post in the forum. If I find it, I'll post it here.

Found it . . . http://forums.westhost.com/phpBB2/viewtopic.php?p=8480#8480

09-02-2004, 09:58 PM
I think htaccess files dont work properly on my site, but I haven't tested them much (or perhaps I was just wrong in my syntax when composing my htaccess). I will test more when I have time to see what I did wrong or what prevents me using htaccess.

09-03-2004, 06:39 AM
You should also be able to put that command in your httpd.conf file inbetween <directory> tags. In fact the apache documentation recommends that.


09-03-2004, 06:40 AM
.htaccess should work on all WH accounts I belive. WH uses it on your site to control access to things like phpMyAdmin.

06-09-2006, 06:06 AM
I use a PHP redirect and it works quite well

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'www';
echo $host.$uri.$extra;
header("Location: http://$host$uri/$extra");