Apache 使用变量简化.htaccess文件中的mod_重写规则

Apache 使用变量简化.htaccess文件中的mod_重写规则,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我继承了一个.htaccess文件,其中包含一组极为复杂的mod_重写规则,大约有100行代码重复,如下所示: RewriteCond %{REQUEST_URI} !^/(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/favicon.ico RewriteCond %{REQUEST_URI} !^/(test|dev|((current|archive|all|2x|19|20

我继承了一个.htaccess文件,其中包含一组极为复杂的mod_重写规则,大约有100行代码重复,如下所示:

RewriteCond %{REQUEST_URI} !^/(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/favicon.ico
RewriteCond %{REQUEST_URI} !^/(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/robots.txt
RewriteRule ^(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/(.*) /index.php?title=$5 [L,QSA,S=1]
请注意,这些规则确实存在细微差异,而且并不完全相同,但它们之间存在一致的组成部分

我想使用变量使这些规则更易于维护,这可能吗?在这个伪示例中,有一行很长的内容:

# define variables
versions=(current|archive|all|2x|19|20|21|22|23|24|25|26|27)
languages=(current|archive|all|2x|19|20|21|22|23|24|25|26|27)
# body of rules
RewriteCond %{REQUEST_URI} !^/(test|dev|($versions/$languages))/favicon.ico
RewriteCond %{REQUEST_URI} !^/(test|dev|($versions/$languages))/robots.txt
RewriteRule ^(test|dev|($versions/$languages)/(.*) /index.php?title=$5 [L,QSA,S=1]

你不能在这样的变量中存储部分正则表达式

作为一种解决方法,如果以下条件为真,则可以设置环境变量:

RewriteRule (current|archive|all|2x|19|20|21|22|23|24|25|26|27) - [E=VERSION:$1]

RewriteRule (current|archive|all|2x|19|20|21|22|23|24|25|26|27) - [E=LANG:$1]
完成后,您可以在规则中使用这些环境变量,例如在您的条件中:

RewriteCond %{ENV:VERSION} .+

RewriteCond %{ENV:LANG} .+