Solved

Redirecting based on condition - PHP

Posted on 2013-01-16
10
254 Views
Last Modified: 2013-01-17
Hi,

I do have the following code in PHP:

When user has entered the following URL ('http://site1.com/knowthecode') then on server side, i do have this code. Please note that knowthecode is not a PHP file. Please follow the code carefully.

<?php

         if($_SERVER['REQUEST_URI'] == 'http://site1.com/knowthecode')
         {
                 header("Location: http://site2.com/knowthecode"); exit;
         }
         else
         {
                 header("Location: http://site3.com/knowthecode"); exit;
         }
?>

This is NOT working as expected. Can you help ?

Thanks
0
Comment
Question by:milani_lucie
10 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 38785674
did you print this variable value, $_SERVER['REQUEST_URI'] ?

so that it redirects correctly.
0
 
LVL 1

Expert Comment

by:SirLagz
ID: 38785680
What is it doing ? 404 error ?
Loads up the index page ?
You may need to do this - notice the ? after the / in the URI
And then visitors will need to go to that url with the ?

<?php 
         if($_SERVER['REQUEST_URI'] == 'http://site1.com/?knowthecode')
         {
                 header("Location: http://site2.com/knowthecode");
         }
         else
         {
                 header("Location: http://site3.com/knowthecode");
         }
?>

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38785823
If 'knowthecode' is Not a PHP file, this

if($_SERVER['REQUEST_URI'] == 'http://site1.com/knowthecode')

Open in new window


can Never be true.  That means it will always take the 'else' path.

$_SERVER['REQUEST_URI'] is the current file that is executing.  Which must be a PHP file for this to even exist.  In addition, $_SERVER['REQUEST_URI'] is only the part starting with the '/' after the domain name which could be '/knowthecode.php' if it was a PHP file.  Another reason it will never be true.
0
Industry Leaders: 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!

 
LVL 9

Expert Comment

by:rinfo
ID: 38786409
$pos = strpos($_SERVER['REQUEST_URI'] ,'knowthecode');
if ($pos > 0)
{
                 header("Location: http://site2.com/knowthecode");
         }
         else
         {
                 header("Location: http://site3.com/knowthecode");
         }
0
 

Author Comment

by:milani_lucie
ID: 38788135
Can you please let me know how to map this

http://site1.com/knowthecode

to

http://site1.com/knowthecode.htm
0
 

Author Comment

by:milani_lucie
ID: 38788243
SirLagz,

Yes ... it is giving 404 error (page not found). I think so it is trying to access file with name

http://site1.com/knowthecode

but there is no file with name knowthecode on server and we do have knowthecode.htm on server. Now here comes the mapping of URL. Can you please let me know what are the steps we need to do to resolve this issue ?

Thanks
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38788347
PHP can't do that because the server has to be able to find the file in the first place.  But Apache and some versions of IIS can remap URLs to files.  Which operating system and web server are you using?

And why can't you use the correct URL and file name in the first place?
0
 

Author Comment

by:milani_lucie
ID: 38788413
Windows Server 2003 R2 and IIS i am using. Thanks
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38788437
0
 

Author Comment

by:milani_lucie
ID: 38788488
I need to do this for only one file. If the request comes as - http://site1.com/knowthecode then i want to redirect to - http://site1.com/knowthecode.html. Can you please provide me the steps as images ?

Thanks
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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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…

756 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