PHP Redirect to the Correct URL

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

LVL 12
ShaneJonesAsked:
Who is Participating?
 
hankknightConnect With a Mentor Commented:
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
 
dr_lionheartCommented:
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
 
ShaneJonesAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
hankknightCommented:
You may wish to consider using mod_rewrite for this instead of php.
0
 
Ray PaseurConnect With a Mentor Commented:
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
 
Ray PaseurConnect With a Mentor Commented:
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
 
hankknightCommented:
Any luck?  If you have any questions about my idea or Ray's idea let us know.

Good luck at your new job.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.