Solved

PHP: Redirect based on host request

Posted on 2014-04-16
13
317 Views
Last Modified: 2014-04-16
I have a page.  If the user types in the following I want them to get a certain page, if not they get normal page.

I have two domains resolving to one website.  This is temporary, but for two weeks I need it to be this way.

For example.

If user comes to my site by typing in "mysite.com", then I want to take them to "mysite.com/play".  If they come by typing the other domain, "mysite2.com", the should just load the page normally.

what would the php for this be?
0
Comment
Question by:a204801
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 1

Expert Comment

by:ceo_tech
ID: 40004897
You just need to point the website to the directory.
Who do you host with?
0
 
LVL 1

Expert Comment

by:ceo_tech
ID: 40004899
***URL(s) to directories***
0
 

Author Comment

by:a204801
ID: 40004901
I think you misunderstand.  I have two domain.  1. mysite.com 2. mysite2.com

They both resolve to the same place.

I want some PHP that looks at the request and decides, based on what they requested where they go.

If they typed in "mysite.com", they should be redirected to "mysite.com/play".  If they typed in "mysite2.com" they should just load the page.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40004936
This is untested code, but it should be about right in principle.  Put this into the index.php file right at the top.  You may want to consider whether www variants of this URL need to be redirected, so you might want to consider writing a bit more code to normalize the HTTP_HOST variable.

<?php
if ($_SERVER['HTTP_HOST'] == 'mysite.com')
{
    header('Location: http://mysite.com/play');
    exit;
}

Open in new window

0
 

Author Comment

by:a204801
ID: 40005085
How could I change it so that its says 'mysite.com' or 'www.mysite.com'?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40005137
Something along these lines.  Obviously I cannot test this because I do not control the web sites, so you have to do the testing, but I think this is pretty close.

<?php

// A REGULAR EXPRESSION TO FIND www. AT THE START OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^'        // START OF STRING
. 'www'      // LITERAL STRING
. '\.'       // ESCAPED DOT
. '#'        // REGEX DELIMITER
. 'i'        // CASE-INSENSITIVE
;

// REMOVE www. IF IT IS THERE
$host = preg_replace($rgx, NULL, $_SERVER['HTTP_HOST']);

// REDIRECT IF NEEDED
if ($host == 'mysite.com')
{
    header("Location: http://$host/play");
    exit;
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40005146
BTW, based on your history grading responses here at EE, you might want to acquaint yourself with the community norms.  Have a look at the grading guidelines, showing here:
http://support.experts-exchange.com/customer/portal/articles/481419
0
 

Author Comment

by:a204801
ID: 40005153
Cant it be a tad simpler?  I was just looking to add something along the lines of this:

if ($_SERVER['HTTP_HOST'] == 'mysite.com' or 'www.mysite')

Would that work?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40005238
It might or might not work, but as written it would fail with a parse error.  Nobody who understood PHP and client/server protocols would write anything like that.  Trying to make the code simpler will probably make it wrong, resulting in intermittent failures.  

Did you test the code sample I posted?

If you're new to PHP and you want to get a foundation in how all of this works, you might find this article helpful.  It will give you some pointers to good learning resources and more importantly, steer you away from the many bad, ancient and incompetent examples of PHP code that litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:a204801
ID: 40005256
I have not tried it.  The only issue I had was that I want www at all times.  At no time should the domain be naked.  I'm actually going to update the .htaccess file to make that so, but Imnot sure how your piece above will affect that?  Im referrig to this:

// REMOVE www. IF IT IS THERE
$host = preg_replace($rgx, NULL, $_SERVER['HTTP_HOST']);
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40005267
Do you know why you want www at all times?  It is a subdomain, and it may or may not matter depending on your application and your server settings.

The process we are trying here is called "normalization."  So we can remove the "www" if it is there, then we have a "naked" domain.  With that we can add the "www" back and thereby avoid www.www.domain, etc.

<?php

// A REGULAR EXPRESSION TO FIND www. AT THE START OF A STRING
$rgx
= '#'        // REGEX DELIMITER
. '^'        // START OF STRING
. 'www'      // LITERAL STRING
. '\.'       // ESCAPED DOT
. '#'        // REGEX DELIMITER
. 'i'        // CASE-INSENSITIVE
;

// REMOVE www. IF IT IS THERE
$host = preg_replace($rgx, NULL, $_SERVER['HTTP_HOST']);

// ADD WWW BACK TO ENSURE IT IS THERE ONLY ONCE
$host = 'www.' . $host;

// REDIRECT IF NEEDED
if ($host == 'www.mysite.com')
{
    header("Location: http://$host/play");
    exit;
}

Open in new window

0
 

Author Comment

by:a204801
ID: 40005283
That solution worked perfectly.  Thank you.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40005292
Glad we got it working.  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question