Apache RewriteRule 与 RewriteMap

发布于 2024-08-15 03:27:10 字数 520 浏览 5 评论 0原文

我有一个 RewriteMap ,如下所示:

Guide           1
Mini-Guide      2
White Paper     3

我将其包含到 Apache 中,通过

RewriteMap legacy txt:/var/www/site/var/rewrite_map.txt

我想创建一个 RewriteRule 来允许只有来自所述 RewriteMap 左侧的值才位于该位置;

RewriteRule ^/section/downloads/(${legacy})/(.*)$ /blah.php?subsection=${legacy:%1}&title=$2

我知道我可以在右侧使用 ${legacy},但是我可以在左侧使用它吗?如果可以,如何使用?

I've got a RewriteMap that looks like this:

Guide           1
Mini-Guide      2
White Paper     3

and I'm including it into Apache via

RewriteMap legacy txt:/var/www/site/var/rewrite_map.txt

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-08-22 03:27:10

在映射文件中,左侧是键,右侧是值。当您创建用于匹配映射的规则时,您输入键,它输出值。

将您的 RewriteRule 更改为:

# Put these on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
            /blah.php?subsection=${legacy:$1}&title=$2

第一个分组捕获传入 URL 中的字符串。替换中的 $1 将其应用于指定的映射。要设置默认值,请将 ${legacy:$1} 更改为 ${legacy:$1|Unknown}

最后,如果您只希望规则对地图文件中的值起作用,请添加一个 RewriteCond

RewriteCond ${legacy:$1|Unknown} !Unknown
# Put these on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
            /blah.php?subsection=${legacy:$1}&title=$2

该条件表示地图是否不返回默认值(未知),然后运行下一条规则。否则,跳过该规则并继续。

Apache RewriteMap

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.

Change your RewriteRule to this:

# Put these on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
            /blah.php?subsection=${legacy:$1}&title=$2

The first grouping captures the string in the incoming URL. The $1 in the replacement applies it to the named map. To make a default value, change ${legacy:$1} to ${legacy:$1|Unknown}.

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.

Apache RewriteMap

热情消退 2024-08-22 03:27:10

另一种变体:

# %1 will be the subpattern number1 afterwards
RewriteCond %{REQUEST_URI} ^/section/downloads/(.*)
# check if there is no mapping for %1
RewriteCond ${legacy:%1} !^$
# if there is rewrite it
RewriteRule ^/(.*) /blah.php?subsection=${legacy:%1}&title=$2 [R]

another variant:

# %1 will be the subpattern number1 afterwards
RewriteCond %{REQUEST_URI} ^/section/downloads/(.*)
# check if there is no mapping for %1
RewriteCond ${legacy:%1} !^$
# if there is rewrite it
RewriteRule ^/(.*) /blah.php?subsection=${legacy:%1}&title=$2 [R]
久光 2024-08-22 03:27:10

您说过,您只想允许在地图中找到的值。除非您在正则表达式中为捕获组指定附加限制,否则这是不可能的。没有办法用地图本身来做到这一点。据我所知,没有“map.keys”语法可以应用于左侧模式。

但是,
如果未找到捕获的值,您可以指定默认值。这样:

## all on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
        /blah.php?subsection=${legacy:$1|defaultValue}&title=$2

将“defaultValue”替换为您喜欢的任何值。例如,如果在地图中未找到给定的参数,则为 0(零)或“notfound”。

然后,您可以使用另一个规则重写结果,或者只是允许它通过并在 URL 上提供带有默认值的“404”消息。

如果您选择使用其他规则,那么它将如下所示:

## all on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
        /blah.php?subsection=${legacy:$1|notFoundMarker}&title=$2

## This rule fires if the lookupKey was not found in the map in the prior rule.
RewriteRule ^/blah.php?subsection=notFoundMarker  /404.php   [L]

You said, you want to only allow values found in the map. This isn't possible unless you specify an additional restriction in regex for the capture group. There's no way to do it with the map itself. There's no "map.keys" syntax, as far as I know, that you can apply in the left hand side, the pattern.

BUT,
You can specify a default value if the captured value is not found. This way:

## all on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
        /blah.php?subsection=${legacy:$1|defaultValue}&title=$2

Replace "defaultValue" with whatever you like. For example 0 (zero), or "notfound", if the given arg is not found in the map.

You can then either rewrite the result of that, with another rule, or just allow it to flow through and provide a "404" message at the URL with the default value.

If you choose to use another rule, then it would look like this:

## all on one line
RewriteRule ^/section/downloads/([a-zA-Z-]+)/(.*)$
        /blah.php?subsection=${legacy:$1|notFoundMarker}&title=$2

## This rule fires if the lookupKey was not found in the map in the prior rule.
RewriteRule ^/blah.php?subsection=notFoundMarker  /404.php   [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文