简体   繁体   English

将多个htaccess重写规则合并为一个

[英]Combining multiple htaccess rewrite rules into one

First, let me say this: I suck at regex and htaccess. 首先,让我这样说:我很喜欢regex和htaccess。 I'm trying to make a rule that will properly parse url segments into variables. 我正在尝试制定一条规则,将URL段正确解析为变量。 So far I have this: 到目前为止,我有这个:

RewriteRule ^([^/]+)/?$ index.php?query[]=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?query[]=$1&query[]=$2&query[]=$3&query[]=$4 [QSA,L]

It works, sort of, but I feel it's longer than it needs to be; 它可以工作,但是我觉得它比需要的更长。 and what if I want 5 or 6 or 7 variables? 如果我想要5或6或7个变量怎么办? Is there a more condensed way to write this out? 有没有更简洁的方式写出来?

Also, when I spit out the query array, the first element is always index.php. 另外,当我吐出查询数组时,第一个元素始终是index.php。 What's up with that? 那是怎么回事?

不要使用RewriteRule将pathinfo转换为查询参数,而只需在Apache HTTP Server中启用MultiViews并在explode()很少帮助下在index.php使用$_SERVER['PATH_INFO']

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

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