简体   繁体   English

htaccess mod_rewrite多个查询字符串的路径

[英]htaccess mod_rewrite multiple paths to query string

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. 我正在尝试找到一种优雅的方式,通过htaccess和mod_rewrite将URL路径重定向到查询字符串。

The problem is that the path does not have a fixed amound of sub dirs and could be huge. 问题在于该路径没有固定的次要路径,并且可能很大。

ie http://example.com/sub1/sub2/sub3/sub4/sub5/sub6/sub7 ... http://example.com/sub1/sub2/sub3/sub4/sub5/sub6/sub7 ...

Currently I just have a load of rules in my htaccess to capture these... 目前,我在htaccess中只有大量规则可以捕获这些规则...

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM