简体   繁体   中英

.htaccess rewrite rules for PHP script

I have structure php url like:

example.com/category.php?cat=graphics-and-design

and I want to clean the url via a .htaccess rewrite rule to:

example.com/category/graphics-and-design

I tried the following code but still doesn't work:

RewriteRule ^([a-zA-Z0-9_-]*)\/(.*)$ category.php?cat=$1 [L,QSA]

If you don't want problems later with other rules, for example not being able to match example.com/project/foobar without it going to category.php?cat=foobar

Then I suggest you match the category as well:

RewriteRule ^category/(.*)$ category.php?cat=$1 [L,QSA]

If you still want to go down that route (pun intended), the problem is its passing the $1 'st match:

example.com/category.php?cat=category

So instead pass match $2

RewriteRule ^([a-zA-Z0-9_-]*)/(.*)$ category.php?cat=$2 [L,QSA]

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