Solved

Redirect Page based on URL

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

705 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

16 Experts available now in Live!

Get 1:1 Help Now