apache mod_rewrite RewriteCond没有按预期忽略文件

时间:2015-12-23 21:57:39

标签: apache .htaccess mod-rewrite

重写引擎已打开(Apache2.2),我正在尝试执行简单的URL重写。重写不应该影响js和其他静态文件。感兴趣的网址:.Row { display: table; /* Let the element behave like a <table> element */ width: 100%; /* expand as far was you then table */ table-layout: fixed; /* The horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells */ border-spacing: 5px; /* padding around the table */ border: 1px solid #000; } .Column { display: table-cell; /* this is a table-cell */ table-layout: auto; /* let the browser do it */ width: 100%; /* expand the cell to maximum size */ } #TerraMT { width: 100%; height: 26; border: none; z-index: 1; } #TerraMB { width: 100%; height: 100%; border: none; z-index: 1; } #AuraMT { width: 100%; height: 26; border: none; z-index: 1; } #AuraMB { width: 100%; height: 100%; border: none; z-index: 1; } #AquaMT { width: 100%; height: 26; border: none; z-index: 1; } #AquaMB { width: 100%; height: 100%; border: none; z-index: 1; } #resizable2 { display: table; /* Let the element behave like a <table> element */ width: 100%; /* expand as far was you then table */ table-layout: fixed; /* The horizontal layout only depends on the table's width and the width of the columns, not the contents of the cells */ border-spacing: 5px; /* padding around the table */ border: 1px solid #000; } .Column2 { display: table-cell; /* this is a table-cell */ table-layout: auto; /* let the browser do it */ width: 100%; /* expand the cell to maximum size */ } #BT { width: 100%; height: 35; border: 1px solid #000; z-index: 1; } #BB { width: 100%; height: 100%; border: 1px solid #000; z-index: 1; } #BNT { width: 100%; height: 35; border: 1px solid #000; z-index: 1; } #BNB { width: 100%; height: 100%; border: 1px solid #000; z-index: 1; } span.CRMissionTitle { font-family: arial,helvetica,verdana; font-size: 16pt; color: #ffffff; background-color: #999999; font-weight: bold; text-align:center } span.ENCTTitle { font-family:arial,helvetica,verdana; font-size:12pt; color:#ffffff; font-weight=bold; text-align:center } span.ENCTGTitle { font-family:arial,helvetica,verdana; font-size:12pt; color:#ffffff; font-weight=bold; text-align:center } span.today { font-family: arial,helvetica,verdana; font-size: 12pt; color: #000000; font-weight: bold; } span.tomorrow { font-family: arial,helvetica,verdana; font-size: 12pt; color: #808080; font-weight: bold; } span.clocklabel { font-family: arial,helvetica,verdana; font-size: 12pt; color: #33ffff; font-weight: bold; } span.illegal { font-family: arial,helvetica,verdana; font-size: 12pt; color: #ffffff; font-weight: bold; } .generic { font-family: arial,helvetica,verdana; } a { text-decoration: none; } a:hover { color: #990000; text-decoration: none: }

这有效:(对.js文件的请求不受重写规则的影响)

http://example.com/lib/js/my-js-file.js

这也有效:(对.js文件的请求不受重写规则的影响)

#exclude files from rewriting
RewriteCond %{REQUEST_FILENAME} !-f

#rewrite rule
RewriteRule ^(.*)$ /index.cfm/$1 [NS,L]

失败:(试图使用多个规则,由于某种原因,RewriteCond不起作用,并且.js文件未加载,而是通过重写规则“拦截”)

# exclude files from rewriting
RewriteCond %{REQUEST_FILENAME} !-f

# special rewrite
RewriteRule ^my-special-keyword/(.*)$ /index.cfm/something/cool/$1 [NS,L]

为什么第三种情况会失败?

1 个答案:

答案 0 :(得分:1)

这很容易解决。问题是RewriteCond是每次重写的,所以条件只是在它之后的重写(你的特殊重写)。 最后一次重写现在没有条件,所以它只是处理请求。

因此,您需要在特殊重写规则下移动重写行,或者在基本规则之上添加另一个。我要做的就是创建一个规则来完全忽略它自己的真实文件。那么你不必为每个重写规则都有一个条件。试试这些规则。

# if the request is a real file  or real directory do nothing
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# else if special rewrite
RewriteRule ^my-special-keyword/(.*)$ /index.cfm/something/cool/$1 [NS,L]

# else basic rule
RewriteRule ^(.*)$ /index.cfm/$1 [NS,L]