URL重写,用于将路径转换为查询字符串 [英] URL rewrite for converting path to querystring
问题描述
我尝试使URL如下所示
I try to make the URL like the following
http://domain/d1/d2/k1/val1/k2/val2/k3/val3
成为
http://domain/index.php?one=d1&two=d2&k1=val1&k2=val2&k3=val3
上面一个和两个是固定键,其余路径用于键值对. 如果在/d1/d2/之后附加了更多的键/值对(三个以上),该如何写出URL重写规则?
Above one and two are fixed keys, the rest of the path is for key-value pairs. When I have more key-value pairs (more than three) attached after /d1/d2/, how do I write out the URL rewrite rules?
更新#1:
以下是我到目前为止的内容.我不能在前两个文件夹的末尾附加动态键值对.
Below is what I have so far. I cannot have a dynamic key value pairs appended at the end of the first two folders.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(css|png|jpe?g|gif)$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/([^/]+) /index.php?one=$1&two=$2 [QSA,L]
我当时正在考虑再添加一个文件夹查询"(请参见下文)以帮助键值对的模式匹配,因为在查询后您知道所有文件夹都是关于键值对的.但是,我无法走得更远.
I was thinking to add one more folder "query" (see below) to help for pattern match for key-value pairs because you know after query all the folders are about key-value pairs. But, I cannot go further.
http://domain/d1/d2/query/k1/val1/k2/val2/k3/val3
推荐答案
一种替代方法是重定向/index.php上的所有请求,并将原始路径附加为查询字符串.然后,您可以使用PHP轻松地检查原始路径的元素.
An alternative would be to redirect all requests at /index.php with the original path appended as the query string. You could then easily examine the elements of the original path using PHP instead.
请参见 https://stackoverflow.com/a/8019730/1554386
这篇关于URL重写,用于将路径转换为查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!