除特定URI以外的所有位置的Apache配置

MrR*_*ing 2 regex location config directive apache2

我需要为“位置指令”应用一些配置。

这些配置必须应用于所有位置,除了某些位置URIs(例如,我不想更改的配置/products,因此,波纹管配置必须应用于除/ products之外的所有位置)

<Location />
 # desired configurations
</Location>
Run Code Online (Sandbox Code Playgroud)

MrR*_*ing 5

这可以通过几种方式完成:

1.使用正则表达式

位置指令中使用regex 匹配所有URI(模式除外)

例如:

<Location ~ "^((?!/product).)*$">
# desired configurations
</Location>
Run Code Online (Sandbox Code Playgroud)
  1. 使用<If>指令:例如:

在目录中使用If:

<If "%{Request_URI} =! '.*/product.*'">
Run Code Online (Sandbox Code Playgroud)

要么

<If "%{Request_URI} =! '^((?!/product).)*$'">
Run Code Online (Sandbox Code Playgroud)
  1. 通过设置变量并使用If和IfDefine

在配置开始时设置一个变量,然后使用指令

SetEnvIf Request_URI ".*/product.*" isProd=1
...
<IfDefine isProd>
...
Run Code Online (Sandbox Code Playgroud)

或者您可以在If指令中使用expr来比较字符串和变量。


有用的链接