除非规则匹配,否则需要重写

我需要重写到另一个主机,除非URL以/ store或/ utils开头

我试图看看Apache文档,但我不能解决如何做到这一点

好,假设你想redirect到一个主机otherhost.example.com这应该做的Apache下的伎俩。

RewriteCond %{REQUEST_URI} !^/(store|utils)/.* RewriteRule (.*) http://otherhost.example.com$1 [R=301,L] 

条件部分说:“如果请求的URI不是以/ store /或/ utils /应用Rewrite开头的话,那么重写规则将会生成一个301redirect,并附加到请求中。

由于ickymettle succintly回应,! 标志表示如果不符合规则的条件,则应该将input视为匹配。 所以,打破语法:

 ! = 'Not' prefix ^ = start of string / = The first forward slash tells the rule to match only if the input string begins with a / | = the pipe symbol denotes 'or' inside the bracketed group section (as in 'store or utils') The second forward slash denotes that the string ends with a forward slash . = any single character * = zero or more of the preceding character (so .* = 0 or more of any character, effectively passing on the rest of the URI 

AddedBytes.com mod_rewrite cheatsheet是死的方便,我把它贴在我坐在旁边的墙上(因为我几乎每天都潜入htaccesses,拼命尝试教我自己mod_rewrite voodoo!)还有一个[关于PerishablePress的很好的详细的文章大量的技巧和窍门等等。关于mod_rewrite的一个好处是它不会被新的版本不断弃用,所以关于这个主题的旧文章仍然是完全有效的:-)

也可能会破产,还提到TheJackol上的mod_rewrite / .htaccess作弊表,它有一些有用的快速代码片段和一些信息。

其他url:

  • perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/
  • thejackol.com/htaccess-cheatsheet/

对不起,我无法超链接他们,我还没有10个代表点,所以系统阻止我超链接所有三个:-(

 RewriteCond URL (?!.*/utils).* RewriteCond URL (?!.*/store).* RewriteRule (.*) http\://{your new host here}/$3 [I,R] 

注意:这是ISAPI_Rewrite的语法,它应该与mod-rewrite兼容。