Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP: Redirect based on host request

Posted on 2014-04-16
13
Medium Priority
?
319 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 111

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 111

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 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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.

715 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