0

I have this rewriting pattern : /blog/post-url-variable-ID, which work fine.

Options +FollowSymlinks
RewriteEngine on
RewriteRule /blog/([a-zA-Z0-9\-]+)-([0-9]+) /post.php?url=$1&id=$2

As the ID is the decisive variable which pick the data from dba, I wanted to avoid duplicate content by manually rewrite URL.

Ex.: /blog/my-new-post-777 have the same content as /blog/anything-written-here-777 because of the ID.

So I wrote this code, which worked PERFECT in local, which redirect straight to the good URL.

<?php include("connect.php");

$result = mysqli_query($con,"SELECT * FROM blog WHERE id = ".($_GET['id']));
$data = mysqli_fetch_array($result);

if($data["url"]!=$_GET["url"]) {
    header("Location: http://www.mywebsite.com/blog/".$data["url"]."-".$data["id"]);          
}

?>

Since I transfered it to my server, it works no more. I also tried to simplify by header:location to the main page directly, but it looks like not working tho.

I realized that when header:location is on the very top of the page, it works fine. But as $variables are under it, it cannot call them.

Any ideas ? I'm pretty lost right now !

G.EGCB
  • 94
  • 10
  • Refactor your code so that nothing is printed before the call to `header()`. Also, I'd recommend turning on error logging (but setting display_errors to off). That way, you'll be able to see all the errors that happened. – T0xicCode Jan 13 '15 at 17:02
  • Hey @T0xicCode sorry for the delay, and many thanks for your answer. You're right, I read a lot about header location redirects and it only works (in general) when nothing's printed before. But I cannot manage how to call the variables in the redirect URL without calling them before. If you have any tips on that, will be grateful ! Thanks ! – G.EGCB Feb 04 '15 at 21:32
  • Given that I haven't seen your whole code base, I can't tell you exactly what to do to fix your code. Usually, removing the end tags for php in "pure code" files, making sure that there are no spaces or new lines before the start tags for php, and making sure that there are no print statements before any potential calls to header() are enough to solve your issue. I suspect it worked on your local machine because it has output buffering turned on in php.ini, but not on your server. – T0xicCode Feb 04 '15 at 21:43

1 Answers1

0

I've found the solution.. well the coding mistake. Thanks to @T0xicCode for bringing it.

header:Location cannot be called after print statements or outputs. In the following case, I was including a php file called connect.php at the very beginning. This file begins with <?php and was ending with an output ?>. As soon as I removed the output that, technically, was before the header:Location call, everything worked.

Thanks !

G.EGCB
  • 94
  • 10