Solved

Redirecting based on condition - PHP

Posted on 2013-01-16
10
252 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
can windows 10 wamp send mail()? 3 34
only allow numbers with preg match 4 26
mysql update statement effect only some rows 4 27
php function to remove a file 25 7
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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…
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…

896 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

11 Experts available now in Live!

Get 1:1 Help Now