如何根据cookie值进行htaccess重定向

发布于 2024-09-28 14:51:22 字数 454 浏览 3 评论 0原文

我在谷歌失败了,在这里搜索找不到答案。抱歉,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-10-05 14:51:22

这适用于像 id=1234 这样的 cookie:

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 中,

它将再次使用,

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

这可能就是无限循环的原因。要解决此问题,请确保 htaccess 规则未应用于子目录或使用另一个目录进行缓存。

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]

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

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

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.

烟凡古楼 2024-10-05 14:51:22

这是其他遇到此问题的人的解决方案:

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

虽然我还没有测试 cookie 部分 - 我确信那里还会有更多问题!但其余的我测试过并且有效! (它会发送给访客,然后不会进入 infi-loop,耶!)

祝您有美好的一天! 8)

Here is the solution for anyone else having this problem:

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文