.htaccess mod_rewrite with multiple variable in url

喜你入骨 提交于 2019-12-12 06:27:24

问题


I'm using a search that create a query string for MySQL and show the result. The thing is that the query doesn't have the same length of variable each time a search is made.

I was wondering if in .htaccess it's possible to make a mod_rewrite depending on how much variable I throw at it.

Instead of having this in the URL

results.php?var1=something&var2=something&var3=.....

I get this

results/var1/something/var2/something/var3/something...

But remember that the numbers of vars can change depending on which checkboxes are checked by the user on the search form.

I hope it's clear if not I'll try to add more example.


回答1:


Try this in your htaccess file in your document root:

RewriteEngine on
RewriteRule ^results/([^/]+)/([^/]+)(.*)$ /results/$3?$1=$2 [L,QSA]
RewriteRule ^results/$ /results.php [L,QSA,R]

The first rule will loop as long as there are /var/something pairs in the URI, and keeps appending them on the query string. When finally there are no more pairs left, the results/ gets rewritten to results.php.



来源:https://stackoverflow.com/questions/12217261/htaccess-mod-rewrite-with-multiple-variable-in-url

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