Solved

Redirecting based on condition - PHP

Posted on 2013-01-16
10
251 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 82

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Re-imbursement Claim System 3 25
windows 10 bash shell 4 35
Wordpress update causing pages to crash 1 19
PHP JSON Clean up 5 6
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

758 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

21 Experts available now in Live!

Get 1:1 Help Now