[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Redirect to the Correct URL

Posted on 2009-02-24
9
Medium Priority
?
396 Views
Last Modified: 2013-12-13
I am testing some code on my site for work (on my second day here), we have an issue where some affiliates come to the url

domain.com/?adnetwork=af

we need this to be

domain.com/index.php?adnetwork=af

the url will not work with the first one because of the way the site is configured.

Iam having trouble getting this below code to work any ideas.

Thanks
<?php
//get current URL
$url = "http" . ((!empty($_SERVER['HTTPS'])) ? s : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
 
if ($url == "http://www.shanedj.com/test/otb/?adnetwork=af"){
	// php redirect to correct url
	header(("Location: http://www.shanedj.com/test/otb/index.php?adnetwork=af"));
}
?>

Open in new window

0
Comment
Question by:ShaneJones
7 Comments
 
LVL 3

Expert Comment

by:dr_lionheart
ID: 23720107
try this
<?php
//get current URL
$url = "http" . ((!empty($_SERVER['HTTPS'])) ? s : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
 
if ($url == "http://www.shanedj.com/test/otb/?adnetwork=af"){
	// php redirect to correct url
	header("Location: http://www.shanedj.com/test/otb/index.php?adnetwork=af");
}
?>

Open in new window

0
 
LVL 12

Author Comment

by:ShaneJones
ID: 23720155
am getting the error

Warning: Cannot modify header information - headers already sent by (output started at /home/shanedj/public_html/test/otb/index.php:3) in /home/shanedj/public_html/test/otb/index.php on line 9
0
 
LVL 16

Expert Comment

by:hankknight
ID: 23720204
You may wish to consider using mod_rewrite for this instead of php.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Accepted Solution

by:
hankknight earned 672 total points
ID: 23720362
Create a file called  .htaccess containing the following code and place it in the same directory as index.php or any other directory you want to test this on.

RewriteEngine On
RewriteRule ^(/)?$ http://www.shanedj.com/test/otb/index.php [L,R=302]

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1344 total points
ID: 23721887
Shane, two things.  First, as you have discovered from the error message, header() statements must come first before any output to the browser, and that includes any whitespace.  A common error is something like the code snippet.  The new line character between those two PHP scripts makes the header() fail.  Sorry, but that's just the way the WWW works.

The second thing is that header() is a synchronous command.  PHP executes it and THE SCRIPT KEEPS RIGHT ON RUNNING!  So if you want to redirect to a new page, you almost certainly want to put "exit" or "die()" right after the header() command.

You might also consider sending a "moved permanently" header along with the redirect.

Best regards, ~Ray
<?php /* NO OUTPUT FROM THIS SCRIPT */ ?>
<?php 
header("Location: /"); // REDIRECT TO THE HOME PAGE
exit;
?>

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1344 total points
ID: 23721929
Sample redirect with "moved permanently" header.  Replace the slash in the location statement with whatever is appropriate to your application.

Best regards, ~Ray
<?php
if ( [SOME CONDITION] ) // IF WE WANT TO REDIRECT
{
   header("HTTP/1.1 301 Moved Permanently"); 
   header("Location: /"); 
   exit; 
}
else // IF WE DO NOT WANT TO REDIRECT
// etc...

Open in new window

0
 
LVL 16

Expert Comment

by:hankknight
ID: 23736272
Any luck?  If you have any questions about my idea or Ray's idea let us know.

Good luck at your new job.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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.
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
Course of the Month18 days, 18 hours left to enroll

834 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