homepage Welcome to WebmasterWorld Guest from 54.83.133.189
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Redirect based on domain
wcruick




msg:4035985
 7:27 am on Dec 3, 2009 (gmt 0)

Folks - I'm brand new to the world of PHP so I need the help of you Gurus out there. I have a website that can be reference by any of 3 name, say

www.domain1.com
www.domain2.net
www.website.com

The first two are parked on the third.

I will like that if someone got the site by one of the domains, they get redirected as follows ...

www.domain1.com -> www.website.com/sect1
www.domain2.net -> www.website.com/sect2
www.website.com -> www.website.com - no redirect.

Any ideas please ?

Thanks and much appreciation.

 

NomikOS




msg:4036069
 10:56 am on Dec 3, 2009 (gmt 0)

3 domains in the same server, right?
put this code in index.php:

if(strstr($_SERVER['HTTP_HOST'], 'domain1.com'))
require_once('sect1/index.hp');
else if(strstr($_SERVER['HTTP_HOST'], 'domain2.net'))
require_once('sect2/index.hp');

gonny




msg:4036163
 2:13 pm on Dec 3, 2009 (gmt 0)

lol just creATE a file named index.php and put it on root of domain1 and domain2.
paste this code on index.php:

Header("location: [website.com...]
exit;

for the second just change header("location: [website.com...]

NomikOS




msg:4036227
 3:52 pm on Dec 3, 2009 (gmt 0)

gonny is OK but that overload server too much. Is not necessary reload everytime.

wcruick




msg:4036513
 10:02 pm on Dec 3, 2009 (gmt 0)

Gonny - if I'm reading you correctly, not sure that would work as you can only have 1 index file as all three domain are mapped to the same space.
NomikOS thanks for the suggestion - I believe this will work.

TheMadScientist




msg:4036556
 11:22 pm on Dec 3, 2009 (gmt 0)

Why not just use mod_rewrite and canonicalize the domains?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule .? http://www.example.com%{REUQEST_URI} [R=301,L]

Also, if you're worried about search engine rankings, neither will do you any good, because they don't server a proper 301 redirect header. It looks like the first response actually just serves the information to the locations requested, which duplicates your content on 3 domains and is generally considered a VERY bad idea for SEO purposes, the second idea is better, but does not serve a 301 header before the redirect...

At the top of your single index.php:

if(strpos($_SERVER['HTTP_HOST'],'example1.com')!==FALSE strpos($_SERVER['HTTP_HOST'],'example2.net')!==FALSE)) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com'.$_SERVER['REQUEST_URI']);
}

TheMadScientist




msg:4036632
 1:47 am on Dec 4, 2009 (gmt 0)

Typo Correction:

%{REQUEST_URI}

rocknbil




msg:4037288
 9:28 pm on Dec 4, 2009 (gmt 0)

Header("location: http://www.example.com/sec1");

This is a very bad idea. While it "works" from the browser, it will do your site tons of damage for ranking.

Also, if you're worried about search engine rankings, neither will do you any good, because they don't server a proper 301 redirect header.

Not sure what you're saying here, the rewrite you posted does issue a 301 permanently moved . . . however, I'd simplify it a little, there may not be a REQUEST_URI.

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>

Like normal rexexps, zero or more of "anything" (.*) gets stored in $1.

Documentation [httpd.apache.org] (Scroll to "flags")

You could also modify the server configuration so mod_rewrite is not required, but this is a little deeper dig you may not have access to. In-page redirection is not how you want to do this, it will definately harm your site's indexing.

NomikOS




msg:4037460
 2:50 am on Dec 5, 2009 (gmt 0)

I've been in the same situation and it worked perfectly. You can even set cookies regardless.
if(strstr($_SERVER['HTTP_HOST'], 'domain1.com'))  
{
ini_set('session.cookie_domain', '.domain1.com');
require_once('sect1/index.hp');
}
else if(strstr($_SERVER['HTTP_HOST'], 'domain2.net'))
{
ini_set('session.cookie_domain', '.domain2.net');
require_once('sect2/index.hp');
}

Sure that it is a Q&D solution, but it works...

TheMadScientist




msg:4037985
 1:27 am on Dec 6, 2009 (gmt 0)

Not sure what you're saying here

I'm saying neither of the previously posted solutions will probably have the desired effect, so I posted a solution (x 2).

however, I'd simplify it a little, there may not be a REQUEST_URI.

When will a REQUEST_URI not be set?
It will either be / or /something to access the site.

My Mod_Rewrite is much more efficient than what you posted because I'm only comparing a single character or 0 characters and not storing what is already set by default...

All you need to do is match 0 or 1 characters to compare all requested locations to the condition(s) in the ruleset.

In the .htaccess a preceding / is not present for the RewriteRule to be matched, because you are already at /, but %{REQUEST_URI} always starts with the preceding /, even in the .htaccess, so I'm not sure how it could not be set or would be missing, because a URI must be requested to access any part of the site, even the root domain. The REQUEST_URI of http://www.example.com/ is /

rocknbil




msg:4038286
 7:16 pm on Dec 6, 2009 (gmt 0)

I'm saying neither of the previously posted..... When will a REQUEST_URI not be set?

I figure that's what you meant, and yeah, was wondering if you'd catch that. :-P More accurate and what I was thinking (that somehow didn't make it to the fingers), sometimes there will be nothing after /.

My only point was anything after {HTTP_HOST} can go in $1 with (.*).

TheMadScientist




msg:4038429
 12:44 am on Dec 7, 2009 (gmt 0)

Got it... and good point.

More accurate and what I was thinking (that somehow didn't make it to the fingers)

I have the same issue sometimes.
The brain thinks it, then my fingers go and do their own thing...
Which definitely hinders communication. Stinking fingers. lol. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved