URL rewrite in .htaccess file

你说的曾经没有我的故事 提交于 2019-12-11 12:30:30

问题


I have two problems in my current .htaccess. The script below works but has a limitation

RewriteRule ^products/([0-9])$ /products/$1/ [R]
RewriteRule ^products/([0-9])/$ /viewad.php?adid=$1

Limitation: if adid, is more than 10. it fails. I want it to have whatever number still there and work. my current solution was to add this everytime the id went higher than range.

RewriteRule ^products/([0-9][0-9])$ /products/$1/ [R]
RewriteRule ^products/([0-9][0-9])/$ /viewad.php?adid=$1 

Could some one refine or explain how to correct this rule to take numbers out of range.

Problem Two. Mapping multiple $_GET variables.

say i had a url like this

http://veepiz.com/africanews.php?app=view&func=viewcategory&categoryid=2&page=1

what rule would i write to have it in this format.

http://veepiz.com/africanews/category/2/page/1

or

http://veepiz.com/africanews/2/1

Thanx a lot for your time


回答1:


First problem:

RewriteRule ^products/([0-9]+)$ /products/$1/ [R]
RewriteRule ^products/([0-9]+)/$ /viewad.php?adid=$1

Or better yet:

RewriteRule ^products/(\d+)/?$ /viewad.php?adid=$1

Problem Two:

RewriteRule ^africanews/(\d+)/(\d+)$ /africanews.php?app=view&func=viewcategory&categoryid=$1&page=$2



回答2:


This fixes problem 2 (thanks to aularon):

RewriteRule ^africanews/(\d+)/(\d+)$ /africanews/$1/$2/ [R]
RewriteRule ^africanews/(\d+)/(\d+)/$ /africanews.php?app=view&func=viewcategory&categoryid=$1&page=$2


来源:https://stackoverflow.com/questions/3653320/url-rewrite-in-htaccess-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!