Solved

PHP: Redirect based on host request

Posted on 2014-04-16
13
308 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
 
LVL 108

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 108

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 108

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 108

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 108

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 108

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
angularls and plnkr 14 18
Converting SQL server date to string 3 15
Jquery autocomplete 10 27
Wordpress Body Class 5 12
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now