如何在.htaccess中重写URL [seo permlink]

时间:2016-03-30 09:28:09

标签: php .htaccess url-rewriting

我的文件是category.php?id=,这是我的代码:

<a href="category.php?id=<?php echo $row['id'];?>"><?php echo $row['name'];?></a>

我想将网址更改为site.com/category/title-here/id。如何用.htaccess重写它?

以下是我.htaccess的{​​{1}}页面,它detail.phpsite.com/detail.php?id=1的工作正常:

site.com/title-here/1

2 个答案:

答案 0 :(得分:1)

对于详细信息页面,您可以使用category前缀添加相同的规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.name.com [NC]
RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]

AddDefaultCharset UTF-8
RewriteBase /
RewriteRule ^(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^category/([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ category.php?title=$1&$id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2
RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1

Options -Indexes

答案 1 :(得分:0)

试试这个

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.name.com [NC]
RewriteRule ^(.*)$ http://name.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/([^\.]+)$ category.php [NC,L]
RewriteRule ^/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/?$ detail.php?title=$1&id=$2

RewriteRule ^([a-zA-Z0-9-/]+)/?$ detail.php?id=$1
Options -Indexes

现在您的网址正在显示http://name.com/category/your-title-here?id=1