开发者

Smart URLs using SQL table variable

开发者 https://www.devze.com 2023-02-08 20:58 出处:网络
I would 开发者_JAVA百科like to know If it is possible to generate smart URLs through Apace mod-rewrite in a PHP website passing the value of a SQL variable.

I would 开发者_JAVA百科like to know If it is possible to generate smart URLs through Apace mod-rewrite in a PHP website passing the value of a SQL variable.

Thanks!


This is an example that would apply to
/Customer/Detail/1234
and map it to
/Customer/Detail.php?ID=1234

RewriteEngine on
RewriteRule ^/Customer/Detail/([0-9]+)$  /Customer/Detail.php?ID=$1 

You may want to consider a good MVC framework for other reasons in addition to automating this one.


Of course you can do this! You'll need to get a table with "slugs" that are connected to the "ugly" URLs. A table with the columns "slug" (index) and "ugly_url" will probably do.

mod_rewrite:

RewriteEngine on
RewriteRule ^/[a-zA-Z0-9-_]+$ redirect.php?slug=$1

redirect.php:

// You'll need to find out to wich ugly URL this Slug belongs to.
// Connect to your database ...
$slug = mysql_real_escape ( (string)$_GET['slug'] );
if ( empty ( $slug ) )
    die ( 'Page not found.' );

$query = mysql_query ( "SELECT `ugly_url` FROM `slugs` WHERE `slug` = $slug LIMIT 1" );
if ( !$query )
    die ( 'Page not found.' );

$fetch = mysql_fetch_array ( $query );

$ugly_url = $fetch['ugly_url'];
header ( "Location: $ugly_url" );
0

精彩评论

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