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
Solved

PHP: Redirect based on host request

Posted on 2014-04-16
13
312 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 109

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 109

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 109

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 109

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 109

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 109

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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