2010-07-08 71 views
2

我發現你不能在下一個Rewrite Cond中使用RewriteCond反向引用作爲模式條件...如何使用重寫規則在cookie中測試日期?

我看起來是正確的,但Tim在評論波紋管中是正確的......這仍然通過時我們超出了cookie中保存的有效日期。

關於如何實際測試值而不是存在的任何想法?

RewriteCond %{HTTP_COOKIE} niftyExpire=([^;\ ]+) 
RewriteRule .*index.html /fake.html [E=niftyExpire:%1,C] 
RewriteCond %{TIME} <{ENV:niftyExpire} 
RewriteRule .*fake.html /pass.html [R,L] 
RewriteRule .*index.html /fail.html [R,L] 

感謝,

-Eric

+0

順便提一句,當'RewriteCond%{TIME} <{ENV:niftyExpire}'應該爲false時,您是否測試了條件? 'mod_rewrite'不會擴展測試模式中的任何*變量,所以我不認爲這會以您期望的方式工作(目前'RewriteCond'應該始終計算爲true)。 – 2010-07-09 05:44:19

+0

您的正確,我實際上只是測試cookie的存在......任何想法?謝謝, – Eric 2010-07-09 13:29:19

+0

無可否認,這對於某種腳本來說確實是一項工作,因爲'mod_rewrite'並不是特別適合這些類型的任務。然而,這並不意味着一個解決方案不存在...所以我會琢磨一下,看看是否有一些瘋狂的規則集可以使這成爲可能,因爲這將是相當整潔(儘管有點不切實際,我敢肯定)。 – 2010-07-10 07:28:13

回答

6

好的,所以事實證明,你理論上可以做到這一點。但是,讓我強調這一點很清楚,在任何情況下都不會這樣做。與使用腳本相比,使用程序支持的RewriteMap(儘管並不總是可行),或者在編輯修改後的mod_rewrite的極端情況下,我只能想象這樣的性能是絕對可怕的,並且由於它具有在每個請求中準備在.htaccess文件中使用時,這會導致不必要的大量開銷。

話雖這麼說,對於概念驗證,132線mod_rewrite褻瀆:

RewriteEngine On 

RewriteCond %{HTTP_COOKIE} niftyExpire=([^;\ ]+) [OR] 
RewriteCond fail ^(fail)$ 
RewriteRule index.html$ /fail.html [E=niftyExpire:%1,E=RW_TSTATUS:%1] 

RewriteCond %{ENV:RW_TSTATUS) !=fail 
RewriteCond %{ENV:niftyExpire} ^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})\d{2}$ 
RewriteRule .* - [E=RW_CYEAR:%1,E=RW_CMON:%2,E=RW_CDAY:%3,E=RW_CHOUR:%4,E=RW_CMIN:%5] 

# Check if current YY*YMMDDHHMM < cookie YY*YMMDDHHMM 
#(applicable only for this decade..but that's good enough) 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} !^(\d{4})\1$ 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} ^\d{2}(\d)\d{3}(\d)\d$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} ^\d{2}(\d)\d{3}(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYY*MMDDHHMM < cookie YYY*MMDDHHMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} !^(\d{4})\1$ 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} ^\d{3}(\d)\d{3}(\d)$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} ^\d{3}(\d)\d{3}(\d)$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYY*MDDHHMM < cookie YYYY*MDDHHMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} !^(\d{2})\1$ 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 ^01$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYM*DDHHMM < cookie YYYYM*DDHHMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} !^(\d{2})\1$ 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MON}%{ENV:RW_CMON} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMM*DHHMM < cookie YYYYMM*DHHMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} !^(\d{2})\1$ 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMMD*HHMM < cookie YYYYMMD*HHMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} !^(\d{2})\1$ 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMMDD*HMM < cookie YYYYMMDD*HMM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} !^(\d{2})\1$ 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 ^(01|[01]2)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMMDDH*MM < cookie YYYYMMDDH*MM 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} !^(\d{2})\1$ 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMMDDHH*M < cookie YYYYMMDDHH*M 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} !^(\d{2})\1$ 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} ^(\d)\d(\d)\d$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

# Check if current YYYYMMDDHHM* < cookie YYYYMMDDHHM* 
RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} !^(\d{2})\1$ 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$ 
RewriteRule .* - [E=RW_TSTATUS:success] 

RewriteCond %{ENV:RW_TSTATUS} !fail|success 
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN} ^\d(\d)\d(\d)$ 
RewriteCond %1%2 !^(\d)\1$ 
RewriteRule .* - [E=RW_TSTATUS:fail] 

RewriteCond %{ENV:RW_TSTATUS} !=fail 
RewriteRule .* /pass.html 

如果任何人有一個更簡單的方法,或者可以建議某種程度上,這可以更簡化,我所有的耳朵。我可能通過寫這個怪物犯了一個危害人類的罪行,但至少我們可以說現在不是不可能的。

+0

哇!感謝您的:-) 我發現我的問題的另一個解決方案無關(檢查對目錄中的會員ID爲每個活動成員的文件) 但這:-) 最佳超級創造性的解決方案, -Eric – Eric 2010-07-20 00:39:46

+0

AHAHAHA - +1在任何情況下都不要這樣做。 – Josh 2010-11-10 21:50:33

1

除了額外.* S IN的最後兩個規則,這對我來說很好。擴展後你期望什麼?

+0

事實證明,mod_rewrite不會擴展測試模式中的任何變量,所以我只能有效地測試cookie的存在......關於如何實際測試值而不僅僅是存在的任何想法? – Eric 2010-07-09 13:33:56