PHP重写规则作为数据库值

时间:2013-10-17 13:02:22

标签: php database .htaccess url-rewriting

我需要改写 www.example.com/folder/35467/title.html

www.example.com/folder/?id=35467

idtitle来自数据库。我将htaccess文件放入“文件夹”中。

2 个答案:

答案 0 :(得分:0)

的.htaccess

<IfModule mod_rewrite.c>

    Options +FollowSymLinks
    RewriteEngine on

############################################
## you can put here your script root folder
## path relative to web root

    RewriteBase /

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

</IfModule>

的index.php

list (,,$id,$title) = explode('/', $_SERVER['REQUEST_URI']);
$link = 'www.example.com/folder/?id='. $id;

答案 1 :(得分:0)

如果htaccess文件将在/folder/,那么您需要这些规则:

RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.*)\.html ?id=$1 [L]

要使用ID查询字符串重定向访问权限,您可以查看$_SERVER['REQUEST_URI']中的index.php变量。

if ( !strpos($_SERVER['REQUEST_URI'],'.html') ) {
    // The request that was made wasn't with the nicer looking URL
    header("Location: /folder/$id/$title.html");
    exit();
}