如何基于cookie值做htaccess的重定向 [英] How to do htaccess redirect based on cookie value

查看:397
本文介绍了如何基于cookie值做htaccess的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我失败了,在谷歌,我无法找到答案搜索这里。对不起,我是一个福利局在htaccess的,它有非常奇怪的语法,是这么难学!

I have failed at Google and I could not find the answer searching here. Sorry, I'm a newb at htaccess and it has really odd syntax and is so hard to learn!

您可以看到我想在这里做...

You can see what I'm trying to do here...

RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^.*user_id=(\d+).*$ [NC]
RewriteRule .* http://localhost/mysite/cache/$1 [R=301,L]
RewriteRule .* http://localhost/mysite/cache/guest [R=301,L]

我缓存的页面为每个用户的访问速度。我想重定向到,如果他们登录的一个cookie的正确的HTML缓存文件夹中,否则,我想要加载的客人缓存。

I'm caching the pages for each user for load speed. I want to redirect to the proper HTML cache folder if they're logged in with a cookie, otherwise I want to load the guest cache.

现在它进入了INFI环。如果我删除[R = ...然后我得到内部服务器错误。

Right now it goes into an infi-loop. If I remove the [R=... then I get internal server error.

请帮助!谢谢!

推荐答案

这适用于像ID的Cookie = 1234:

This works for a cookie like id=1234:

的RewriteCond%{HTTP_COOKIE} ^ n =([0-9] *)$ [NC]

RewriteCond %{HTTP_COOKIE} ^id=([0-9]*)$ [NC]

重写规则* 的http://本地主机/ mysite的/缓存/%1 [R = 301 ,L]

RewriteRule .* http://localhost/mysite/cache/%1 [R=301,L]

重写规则* 的http://本地主机/ mysite的/缓存/来宾 [R = 301,L ]

RewriteRule .* http://localhost/mysite/cache/guest [R=301,L]

现在您的问题: 请确保您的htaccess并不适用于你重写页面!例如,如果你的.htaccess在于

Now for your problem: Make sure that your htaccess does not apply to the page you rewrite to! For example, if your .htaccess lies in

/ mysite的/ htaccess的

"/mysite/.htaccess"

它将被再次使用的

的http://本地主机/ mysite的/缓存/%1

"http://localhost/mysite/cache/%1"

那也许是因为你的无限循环的原因。要解决这个问题,确保htaccess的规则并不适用于子目录或使用其他目录缓存。

Thats maybe the reason for your infinite loop. To resolve this, either make sure the htaccess rules are not applied to the subdirectories or use another directory for the cache.

希望有所帮助。

干杯

这篇关于如何基于cookie值做htaccess的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆