使用 Apache 检查 cookie 并重定向

发布于 2024-11-07 17:55:34 字数 677 浏览 0 评论 0原文

我很想得到一些关于此事的反馈。我不确定这是否是正确的方法。

详细信息

我正在运行带有 PHP 5.3/MySQL 4 的 Apache 2,平台为 Drupal 6。

我正在开发一个网站,其中包含几个选定城市的餐厅评论。 当用户到达该站点时,它可以选择哪个城市属于他们。我将他们的选择存储在 cookie 中,如果他们没有做出选择,我会选择一个默认城市。

建议的解决方案

现在,我希望 URL mydomain.com/reviews 根据他们的城市选择重定向到城市特定的 URL。例如,如果我选择巴黎作为我的城市,则为 mydomain.com/reviews/paris。 (如果没有设置 cookie,它应该重定向到默认城市。)

我认为这是最好的选择,因为我希望用户能够在不更改其城市的情况下查看另一个城市的评论。如果他们想查看伦敦餐厅的评论,只需访问 mydomain.com/reviews/london。

为了获得最佳性能,我正在考虑让 Apache 检查 cookie,并在用户访问 mydomain.com/reviews 时重定向到正确的城市。

这是我的问题...

  1. 我如何配置 Apache 来做到这一点?
  2. 这是最好的方法吗?

I'd love to get some feedback on this. I'm not sure if it's the right approach.

The details

I'm running Apache 2 with PHP 5.3/MySQL 4 and Drupal 6 is the platform.

I'm developing a site which contains restaurant reviews in a couple of selected cities.
When the users arrives at the site it can choose which city is theirs. I store their choice in a cookie and if they haven't made a choice I've selected a default city.

Proposed solution

Now I want the URL mydomain.com/reviews to redirect to the city specific URL based on their city choice. For example mydomain.com/reviews/paris if I've selected Paris as my city.
(If there's no cookie set it should redirect to the default city.)

I consider this the best alternative because I want the user to be able to see reviews in another city without changing their city. If they'd like to view reviews for London restaurant they can simply go to mydomain.com/reviews/london.

For the best performance I'm thinking of having Apache check the cookie and make the redirect to the right city when the user goes to mydomain.com/reviews.

So here are my questions…

  1. How do I configure Apache to do this?
  2. Is this the best way to go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

执手闯天涯 2024-11-14 17:55:34
  1. 要配置 Apache 来执行此操作,请使用以下命令,并替换为 cookie 名称。

    RewriteEngine 开启
    RewriteCond %{REQUEST_URI} ^/reviews/?$
    RewriteCond %{HTTP_COOKIE} =([^;]+)
    RewriteRule .* http://mydomain.com/reviews/%1 [R=302,L]
    
  2. Yelp 将位置存储在 cookie 中,所以我认为这是一个好兆头,因为他们有大量流量并且似乎表现良好。

使用 Apache 进行重定向有优点和缺点,但主要缺点是在代码中而不是在服务器上维护重写规则更容易。您可以快速进行修复和部署,而不必更改和重新启动所有 Apache 服务器。

  1. To configure Apache to do this, use the following, replacing with the cookie name.

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/reviews/?$
    RewriteCond %{HTTP_COOKIE} <cookie>=([^;]+)
    RewriteRule .* http://mydomain.com/reviews/%1 [R=302,L]
    
  2. Yelp stores the location in a cookie, so I'd take that as a good sign, since they have a ton of traffic and appear to be doing well.

There are pros and cons to using Apache to do the redirect, but the main con, is that it is easier to maintain the rewrite rule in your code instead of on the server. You can quickly make fixes and deploy, instead of having to change and restart all Apache servers.

剧终人散尽 2024-11-14 17:55:34

如果没有直接在重写规则中设置cookie,还有一种方法可以给出自定义默认值!

%1 始终指的是最后评估的 RewriteCond
如果找到 cookie,则忽略第二个 RewriteCond
如果没有找到,则评估第二个。它所做的只是给出一个值,然后匹配整个值!

RewriteEngine on
RewriteCond %{HTTP_COOKIE} cookiename=([^;]+) [OR]
RewriteCond defaultvalue (.*)
RewriteRule ^(.*)$ /mypath/%1 [L,R=302]

您可以添加其他 RewriteCond 检查,但这必须是最终 RewriteRule 之前的最后两行

There is also a way to give a custom default value if no cookie is set directly in the rewrite rules!

The %1 always refers to the last RewriteCond evaluated!
If a cookie is found, the second RewriteCond is ignored.
If none is found, the second one is evaluated. All it does is to give a value and then match this whole value!

RewriteEngine on
RewriteCond %{HTTP_COOKIE} cookiename=([^;]+) [OR]
RewriteCond defaultvalue (.*)
RewriteRule ^(.*)$ /mypath/%1 [L,R=302]

You can add other RewriteCond checks, but this have to be the last two lines before the final RewriteRule

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