开发者

SEO Url Redirect/Rewrite Using Database Managed Links

I have searched left and right.

And I am trying to find a script or a method on how I can store links in database and use them for redirection.

This is an E-commerce project and I am looking for something to manage product and system URL from database.

Something similar like magento does.

Basically anything af开发者_JAVA百科ter the domain say like domain.com/demo/12/my_iphone

so now demo/12/my_iphone will be sent to database for query and in databases there will be a destination URL which could be productlisting.php?searchstring=iphones

The user will see link domain.com/demo/12/my_iphone but actually he will be seing productlisting.php?searchstring=iphones

Basically demo/12/my_iphone = productlisting.php?searchstring=iphones

And tomorrow if the user wants to edit demo/12/my_iphone to demo/12/myiphone he can just do so using simple form which will update in the database.

How can I achieve this?


Use mod_rewrite in apache in combination with a PHP script.

1) Make a .htaccess file and put it in a folder, say www.yourdomain.com/a/.htaccess

Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteRule .* index.php?%{QUERY_STRING} [L]

2) Create an index.php that handles all requests

(psaudo-code)

<?php
/**
 * This script is simply parsing the url used, /demo/12/my_iphone
 */
require_once('library.php');
$request_uri = $_SERVER['REQUEST_URI'];

// parse $request_uri by splitting on slashes / 
$params = my_parse_function($request_uri); // <= parse_url() is helpful here!

$param1 = $params[0]; // "demo"
$param2 = $params[1]; // "12";       
$param3 = $params[2]; // "my_iphone";

// db-lookup based on the request
$url = fetchUrlFromDB($param1, $param2, $param3);
if($url){
    header('Location: '.$url); // $url = "productlisting.php?searchstring=iphones"
    die();
}
else{
     echo "invalid parameters";
}
?>


It wouldn't be difficult to program such a solution if it comes to that. You could grab the uri from $_SERVER['REQUEST_URI'] and pass it to parse_url to grab the path. Then use the path to query against the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜