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

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

问题描述

我在Google上失败,我在这里找不到答案。对不起,我是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]

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

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-loop。如果我删除[R = ...然后我得到内部服务器错误。

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

请帮助!谢谢!

推荐答案

这适用于 id = 1234

RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^id=([0-9]*)$ [NC]
RewriteRule .* http://localhost/mysite/cache/%1 [R=301,L]
RewriteRule .* http://localhost/mysite/cache/guest [R=301,L]

现在为您的问题:
确保您的htaccess不适用到您重写的页面!例如,如果.htaccess位于 /mysite/.htaccess

p>

It will be used again in

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

这可能是你的无限循环的原因。要解决此问题,请确保htaccess规则不应用于子目录或使用其他目录作为缓存。

That's maybe the reason for your infinite loop. To resolve this, either make sure the htaccess rules are not applied to the sub-directories or use another directory for the cache.

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

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