2012-02-09 15 views
1

我試圖處理邏輯上簡單的操作,但Apache重寫不處理我的網站的查詢字符串請求。我聽說它最終沒有做到。仍然無法理解它的正則表達式語法。Apache重寫爲404,除了匹配的查詢

RewriteEngine on 
RewriteBase/
#RewriteCond %{QUERY_STRING} ^\?category=([a-z])$ // WHY DO I NEED THIS? 
RewriteRule ^!(?category=[a-z]+|?do=[a-z]+)$ [R=404,L,NC] 

目標是向客戶端發送至404對任何查詢不匹配/?類別= [A-Z] +和/?做= [A-Z] +。如果可以從文本文件加載模式將會好得多。

Apache的文檔很差,Google也沒有幫助。

回答

4

請勿在RewriteRule內包含查詢字符串。它匹配在RewriteCond

RewriteEngine on 
RewriteBase/

# For js & css 
# Normally this would be done with: 
# RewriteCond %{REQUEST_FILENAME} !-f 
# But since you might have files here you're trying to put a 404 on, 
# we'll do it differently, matching css & js files 
RewriteCond %{REQUEST_FILENAME} !^(.*)\.(css|js)$ 

#Conditions check if either query param is missing (negated with !) 
RewriteCond %{QUERY_STRING} !category=([a-z]+) 
RewriteCond %{QUERY_STRING} !do=([a-z]+) 

# Rewrite anything not matched by the querystring conditions above... 
# Edit -- was missing the rewrite target (-) 
RewriteRule ^(.*)$ - [R=404,L] 
+0

你好downvoter,請發表評論。 – 2012-02-09 13:44:12

+0

現在我遇到了麻煩,允許/ js /和/ styles/RewriteCond%{PATH_INFO}!styles /([a-z0-9] +)RewriteCond%{PATH_INFO}!js /([a-z0-9] + ) – 2012-02-09 13:47:47

+0

明白了......用REQUEST_URI – 2012-02-09 13:51:15