Solved

Redirect Page based on URL

Posted on 2014-02-20
5
8 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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

911 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

19 Experts available now in Live!

Get 1:1 Help Now