简体   繁体   中英

How to rewrite URL in .htaccess [seo permlink]

My file is category.php?id= and here is my code:

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

I want to change url to site.com/category/title-here/id . How can I rewrite it with .htaccess ?

Here is my .htaccess for detail.php page and it is working fine as site.com/detail.php?id=1 to site.com/title-here/1 :

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

AddDefaultCharset UTF-8
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

As for detail page you can add the same rule with a category prefix:

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

Try This

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

Now Your URL is Showing Like this http://name.com/category/your-title-here?id=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM