检查 cookie 并使用 Apache 重定向 [英] Check cookie and redirect with Apache

查看:25
本文介绍了检查 cookie 并使用 Apache 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想得到一些反馈.我不确定这是否是正确的方法.

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

详情

我使用 PHP 5.3/MySQL 4 运行 Apache 2,Drupal 6 是平台.

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

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

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.

建议的解决方案

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

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.)

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

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.

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

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.

这里是我的问题……

  1. 如何配置 Apache 以执行此操作?
  2. 这是最好的方式吗?

推荐答案

  1. 要配置 Apache 以执行此操作,请使用以下内容,替换为 cookie 名称.

  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]

  • Yelp 将位置存储在 cookie 中,所以我认为这是一个好兆头,因为他们有大量的流量并且看起来表现不错.

  • 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.

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

    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.

    这篇关于检查 cookie 并使用 Apache 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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