如何根据cookie值进行htaccess重定向
我在谷歌失败了,在这里搜索找不到答案。抱歉,我是 htaccess 的新手,它的语法非常奇怪,而且很难学!
您可以在这里看到我正在尝试执行的操作...
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 缓存文件夹,否则我想加载来宾缓存。
现在它进入了 infi 循环。如果我删除 [R=... 那么我会收到内部服务器错误。
请帮忙!!!谢谢你!!!
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]
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.
Right now it goes into an infi-loop. If I remove the [R=... then I get internal server error.
Please help!!! THank you!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这适用于像
id=1234
这样的 cookie:现在解决您的问题:
确保您的 htaccess 不适用于您重写的页面!例如,如果您的 .htaccess 位于
/mysite/.htaccess
中,它将再次使用,
这可能就是无限循环的原因。要解决此问题,请确保 htaccess 规则未应用于子目录或使用另一个目录进行缓存。
This works for a cookie like
id=1234
: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
It will be used again in
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 部分 - 我确信那里还会有更多问题!但其余的我测试过并且有效! (它会发送给访客,然后不会进入 infi-loop,耶!)
祝您有美好的一天! 8)
Here is the solution for anyone else having this problem:
Although I haven't tested the cookie part yet - I'm sure there will be many more problems there! But the rest I tested and it works! (it goes to guest and then does not go into infi-loop, yay!)
Have a great day! 8)