htaccess mod_rewrite查询字符串的多个路径 [英] htaccess mod_rewrite multiple paths to query string
问题描述
我一直在搜索,但是找不到我想要的东西. 我正在尝试找到一种优雅的方法,通过htaccess和mod_rewrite将URL路径重定向到查询字符串.
I've been searching but I can't quite find what I'm after. I'm trying to find an elegant way to redirect my url path to the query string via htaccess and mod_rewrite.
问题在于该路径没有固定的次要路径,并且可能很大.
The problem is that the path does not have a fixed amound of sub dirs and could be huge.
即 http://example.com/sub1/sub2/sub3/sub4/sub5 /sub6/sub7 ...
目前,我在htaccess中只有大量规则可以捕获这些规则...
Currently I just have a load of rules in my htaccess to capture these...
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4&sub[]=$5 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3&sub[]=$4 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2&sub[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/? /index.php?sub[]=$1&sub[]=$2 [QSA,L]
RewriteRule ^([^/]+)/? /index.php?sub[]=$1 [QSA,L]
...但是可能还有更多的人知道自动将其解析为查询字符串的方法吗?
... but there may be more, anyone know of a way to autoamtically parse these in to the query string?
推荐答案
如果参数名称始终相同,则可以依靠重写引擎遍历所有参数:
If the paramater name is always the same, you could just rely on the rewrite engine to loop through them all:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?sub[]=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ /$2?sub[]=$1 [L,QSA]
这篇关于htaccess mod_rewrite查询字符串的多个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!