URL Rewriting - Avoid duplicate content by manual rewriting

139 views Asked by At

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 !

1

There are 1 answers

0
G.EGCB On BEST ANSWER

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 !