简体   繁体   English

htaccess mod_rewrite合并

[英]htaccess mod_rewrite consolidation

Is there a way to consolidate the following two lines into a single line ? 有没有办法将以下两行合并为一行?

RewriteRule . /pages/article.php [L]
RewriteRule $ /pages/article.php [L]

Since it is a similar issue (consolidation of htaccess directives), I also need assistance consolidating the following into a single line as well : 由于这是一个类似的问题(整合htaccess指令),所以我也需要帮助将以下内容整合为一行:

SetEnvIfNoCase Referer "^http://www.example.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.example.com$" locally_linked=1
SetEnvIfNoCase Referer "^http://example.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://example.com$" locally_linked=1

Thanks in advance. 提前致谢。

The two RewriteRule directives can be combined, mainly because the second doesn't do anything especially useful. 可以组合使用两个RewriteRule指令,主要是因为第二个指令没有做任何特别有用的事情。 The $ anchor will always be present in the input string, so that rule matches any input . $锚将始终出现在输入字符串中,因此该规则与任何input匹配。 The first rule only has a single . 第一条规则只有一个. which matches any one character, but since it has no ^$ anchors, that one character can be anywhere in the string. 匹配任何一个字符,但由于它没有^$锚,因此该字符可以在字符串中的任何位置。

Essentially the sum of these two is to match any string with or without a character. 本质上,这两个之和是要匹配任何带或不带字符的字符串。 That can be reduced to: 可以简化为:

RewriteRule .* /pages/article.php [L]

The .* will match zero or more characters, but not capture them for reuse in a variable. .*将匹配零个或多个字符,但不捕获它们以在变量中重用。 There's no real need to test the $ anchor. 真正不需要测试$锚。


The SetEnvIfNoCase can be all combined if you make use of an optional grouping ()? 如果您使用可选的分组()?可以将SetEnvIfNoCase全部合并()? and make the trailing slash optional with /? 并使用/?使斜杠为可选/? . This should match all 4 possibilities, since the www. www.以来,这应该与所有4种可能性匹配www. is made optional, and the trailing / is optional. 是可选的,尾部的/是可选的。

SetEnvIfNoCase Referer "^http://(www\.)?example\.com/?$" locally_linked=1

Note, I have escaped the . 请注意,我已经逃脱了. in the regular expression since they are otherwise interpreted as metacharacters, even though technically the also match the literal . 在正则表达式中使用,因为从技术上讲,它们在其他方面也被解释为元字符,即使从技术上讲,它们也与文字匹配. And the double quotes surrounding the regular expression should not actually be needed, but aren't harmful either. 正则表达式周围的双引号实际上并不需要,但也无害。

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

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