2016-07-07 45 views
0

我在我的Apache配置文件中有以下Rewrite代码,但我无法让它识别何时存在cookie。我在这里看到了一堆答案,但似乎找不到解决我遇到的任何问题的答案。HTTP_COOKIE RewriteCond失败

RewriteEngine on 
RewriteCond %{HTTP_COOKIE} ^returnvisitor$ [NC] 
RewriteRule .? - [S=3] 
RewriteRule /index.html /new_visitor.html 
RewriteRule .* - [CO=returnvisitor:yes:.127.0.0.1:1440:/:true:true] 
RewriteRule .? - [S=1] 
RewriteRule /index.html /returning_visitor.html 

更新1:

我已经修改了代码如下,并取得了一些进展,但仍然不采取行动,因为它应该。

RewriteEngine on 
RewriteCond %{HTTP_COOKIE} returnvisitor=yes [NC] 
RewriteRule .? - [S=3] 
RewriteRule /index.html /new_visitor.html [R] 
RewriteRule .* - [CO=returnvisitor:yes:.127.0.0.1:1440:/:true:true] 
RewriteRule .? - [S=1] 
RewriteRule /index.html /returning_visitor.html [R,L] 

原代码会重定向网页(即默认页面是index.html),但是,现在我在的URL重定向触发手动请求index.html,但至少这个时候的第二个请求我被重定向到returning_visitor.html

我需要通过example.com访问首页访问者发送到new_visitor.html并随后的访问被重定向到returning_visitor.html时访问的主页发生重定向。无需手动输入example.com/index.html

+0

是你的cookie只是一个名字,没有价值?试试'^ returnvisitor ='而不是? –

+0

@MarcB我正在用'RewriteRule。* - [CO = returnvisitor:yes:.127.0.0.1:1440:/:true:true]'代码行首次创建cookie。我检查了我的浏览器,并且正在创建cookie,它只是在'RewriteCond'中没有被检测到。我确实尝试了这个条件以及'/^(.*;)?returnvisitor = yes(; *。)?$ /' – defaultNINJA

回答

1

您可以使用:

RewriteEngine on 
#First visit 
# if the cookie is not set 
RewriteCond %{HTTP_COOKIE} !returnvisitor=yes$ [NC] 
# serve "/new_visitor.html" 
RewriteRule /index.html /new_visitor.html [R,L] 
#second visit 
#set the cookie "returnvisitor" on any uri 
RewriteRule .* - [CO=returnvisitor:yes:.127.0.0.1:1440:/:true:true] 
#check to see that the cookie is set 
RewriteCond %{HTTP_COOKIE} returnvisitor=yes$ [NC]  
#if so, redirect the index.html to "/returning_visitor.html" 
RewriteRule /index.html /returning_visitor.html [R,L] 

让我知道它是如何为你的作品。