Solved

Redirecting based on condition - PHP

Posted on 2013-01-16
10
255 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
[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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 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