Solved

Redirect Page based on URL

Posted on 2014-02-20
5
9 Views
Last Modified: 2016-05-14
I am looking for a piece of code which will take just the domain of the page the code is loaded on and compare it to a fixed domain. If the 2 match I need to redirect the page to a location of my choice.

Can you help?
0
Comment
Question by:MayoorPatel
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39873364
Sure.  Please tell us what the fixed domain is, and what redirect you want.

In PHP you can (almost always) find the value you need in $_SERVER['HTTP_HOST']
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39873367
You can try this:
$ref_server = "www.yourdomain.com";
$this_server = $_SERVER[SERVER_NAME];
if ($ref_server == $this_server)
{
    header('Location: mypage.php');
}

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 41593989
I've requested that this question be closed as follows:

Accepted answer: 500 points for marqusG's comment [url="https://www.experts-exchange.com/questions/28369704/Redirect-Page-based-on-URL.html#a39873367"]#a39873367[/url]

for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Marco Gasi (http:#a39873367)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

tagit
Experts-Exchange Cleanup Volunteer
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41593990
For anyone coming across this answer in the future, here are some things to consider.  

1. The $ref_server variable may not match the $this_server variable.  There would be a mismatch if the client requested the page without www.  It is necessary to normalize the domains before making a comparison.  URLs are not case-sensitive, so a request to www.YourDomain.com would be routed correctly, but would cause this comparison to fail.

2. The string SERVER_NAME is not a constant; it is a string, intended to be an associative array index.  But because the code snippet omits the quotes around the string, it will be tried as a constant first, then a Notice will be issued, then it will be retried as a string array index.  This may cause spurious browser output, and the browser output will cause the script to fail.

3. After the if() statement is run, whether or not the header() function is called, this script will keep right on running, perhaps for an unpredictable period of time until the browser receives the location and disconnects from the server.  Standard practice to prevent this is to put die() or exit immediately after a header() that causes redirection.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

785 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