Solved

Redirect Page based on URL

Posted on 2014-02-20
5
10 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 43

Expert Comment

by:Rob
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 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