阿帕奇重写规则与RewriteMap指令 [英] Apache RewriteRule with RewriteMap
问题描述
我有一个 RewriteMap指令
,看起来像这样:
I've got a RewriteMap
that looks like this:
Guide 1
Mini-Guide 2
White Paper 3
和我,包括它变成的Apache
通过
RewriteMap legacy txt:/var/www/site/var/rewrite_map.txt
我想创建一个重写规则
,允许从上述左侧 RewriteMap指令
是只值这个位置;
I want to create a RewriteRule
that will allow only values from the left side of said RewriteMap
to be in this position;
RewriteRule ^/section/downloads/(${legacy})/(.*)$ /blah.php?subsection=${legacy:%1}&title=$2
我知道我可以使用 $ {遗留}
在右侧,但我可以用它在左边,如果是这样,怎么样?
I know I can use ${legacy}
on the right side, but can I use it on the left, and if so, how?
推荐答案
在您的地图文件,左侧是关键,右边是值。当您创建一个规则对地图匹配,输入你的关键,它输出的价值。
In your map file, the left side is the key and the right side is the value. When you create a rule for matching against a map, you input the key and it outputs the value.
您重写规则改成这样:
# Put these on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
/blah.php?subsection=${legacy:$1}&title=$2
第一个分组捕获传入URL字符串。在更换$ 1中适用于命名映射。为了使默认值,更改 $ {遗产:$ 1}
到 $ {遗产:$ 1 |未知}
最后,如果你只希望规则上是在映射文件中的值工作,添加一个的RewriteCond
:
Finally, if you only want the rule to work on values that are in the map file, add a RewriteCond
:
RewriteCond ${legacy:$1|Unknown} !Unknown
# Put these on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
/blah.php?subsection=${legacy:$1}&title=$2
条件说,如果地图不返回默认值(未知
),然后运行下一个规则。否则,跳过规则,继续前进。
The condition says if the map does not return the default value (Unknown
), then run the next rule. Otherwise, skip the rule and move on.
这篇关于阿帕奇重写规则与RewriteMap指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!