问题
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.
i.e. http://example.com/sub1/sub2/sub3/sub4/sub5/sub6/sub7...
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?
回答1:
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]
来源:https://stackoverflow.com/questions/28699094/htaccess-mod-rewrite-multiple-paths-to-query-string