如何根据 cookie 值进行 htaccess 重定向 [英] How to do htaccess redirect based on cookie value

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

问题描述

我在 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]

我正在为每个用户缓存页面以提高加载速度.如果他们使用 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.

请帮忙!!!谢谢!!!

Please help!!! THank you!!!

推荐答案

这适用于 id=1234 之类的 cookie:

This works for a cookie like 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

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

它会在

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.

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

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