问题
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