Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Redirecting based on condition - PHP

Posted on 2013-01-16
10
Medium Priority
?
259 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 84

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 84

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
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.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

773 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