Solved

Redirect Page based on URL

Posted on 2014-02-20
5
12 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
[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
  • 2
5 Comments
 
LVL 110

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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

687 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