My links are like "http://www.shinylook.ro/product.php?id=58" and I rewrite them like
"http://www.shinylook.ro/produs/58/saboti-dama/"
using this code:
RewriteRule ^produs/([0-9]+)/([a-zA-Z0-9_-]+)\/ product.php?id=$1 [L]
But I cannot manage to redirect the old links to the new ones. If someone will type the old, to be redirected to the new ones.
I tried this one:
RewriteRule ^product.php?id=$1 produs/([0-9]+)/([a-zA-Z0-9_-]+)\/ [L,R=301]
But it doesn't work.
How can I fix this problem?
EDIT:
I managed to write the PHP code... You can try it at www.shinylook.ro/redirect_old_links.php?id=44
.
But I can`t manage to solve the htaccess rewrite code to redirect to that file...
I used RewriteRule ^product.php?id=([0-9]+) redirect_old_links.php?id=$1 [R=301,NC]
But it doesn't work.
EDIT 2:
I managed to solve the problem... the htaccess looks like
RewriteCond %{ENV:REDIRECT_STATUS} !=200
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^product.php$ /redirect_old_links.php/ [L]
RewriteRule ^produs/([0-9]+)/([a-zA-Z0-9_-]+)\/$ product.php?id=$1 [L]
And the PHP code looks like:
<?
include "_storescripts/connect_to_mysql.php";
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_GET['id'])) {
$produs = $_GET['id'];
$sql = mysql_query("SELECT nume_produs FROM products WHERE id='$produs'"); // Chose the product
// ------- We check if it exists ---------
$existCount = mysql_num_rows($sql); // Count
if ($existCount == 1) {
while($row = mysql_fetch_array($sql)){
$nume_brut = $row["nume_produs"];
$nume = explode(" ", $nume_brut);
}
$nume_final = strtolower($nume[0]."-".$nume[1]);
$name = urlencode($nume_final);
header("location: http://www.shinylook.ro/produs/$produs/$name/", TRUE, 301);
}
else {
echo ("This product id does not exist!");
exit();
}
}
?>
If anyone see some mistakes (I'm new to this domain), I will be very glad to hear them and learn how to do this things better...
You probably want to redirect the old links to the new URL so that people use the new links when pasting them on blogs and such.
When using RewriteRule
s, Apache cannot guess the rest of the name in order to do the other part of the redirection.
It can remove it:
produs/58/saboti-dama/ -> product.php?id=58
but where would it get the name again in order to do the reverse?
For this, you should write a redirect_old_links.php
file, which would just redirect to the new location:
http://php.net/manual/en/function.header.php
Example:
User visits
product.php?id=58
RewriteRule product.php?id=([0-9]+) redirect_old_links.php?id=$1 [L]
In redirect_old_links.php
:
mysql_select("SELECT name FROM PRODUCTS WHERE ID = ".$_GET["id"]);
// the above line is just so you get the point,
// please create a proper query with PDO and proper escaping
$id = urlencode($product_id);
$name = urlencode($product_name);
header("Location: produs/$id/$name/");
Sorry, I haven't used PHP in a really long time, and you have to check the documentation for the functions yourself, I don't remember teh mysql_select/fetch procedure. Urlencode may be wrong as well.
But I hope you get the point.