>我需要重写 www.example.com/folder/35467/title.html
www.example.com/folder/?id=35467
id
和title
来自数据库。我要把htaccess文件放到"文件夹"中。
如果 htaccess 文件将出现在/folder/
中,那么您需要以下规则:
RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.*)'.html ?id=$1 [L]
要使用 id 查询字符串重定向访问,您可以在 index.php
中检查 $_SERVER['REQUEST_URI']
变量。
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();
}
.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>
索引.php
list (,,$id,$title) = explode('/', $_SERVER['REQUEST_URI']);
$link = 'www.example.com/folder/?id='. $id;