Solved

PHP: Redirect based on host request

Posted on 2014-04-16
13
314 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

735 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