使用 Apache 检查 cookie 并重定向
我很想得到一些关于此事的反馈。我不确定这是否是正确的方法。
详细信息
我正在运行带有 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 时重定向到正确的城市。
这是我的问题...
- 我如何配置 Apache 来做到这一点?
- 这是最好的方法吗?
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…
- How do I configure Apache to do this?
- Is this the best way to go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要配置 Apache 来执行此操作,请使用以下命令,并替换为 cookie 名称。
Yelp 将位置存储在 cookie 中,所以我认为这是一个好兆头,因为他们有大量流量并且似乎表现良好。
使用 Apache 进行重定向有优点和缺点,但主要缺点是在代码中而不是在服务器上维护重写规则更容易。您可以快速进行修复和部署,而不必更改和重新启动所有 Apache 服务器。
To configure Apache to do this, use the following, replacing with the cookie name.
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.
如果没有直接在重写规则中设置cookie,还有一种方法可以给出自定义默认值!
%1 始终指的是最后评估的
RewriteCond
!如果找到 cookie,则忽略第二个
RewriteCond
。如果没有找到,则评估第二个。它所做的只是给出一个值,然后匹配整个值!
您可以添加其他
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!
You can add other
RewriteCond
checks, but this have to be the last two lines before the finalRewriteRule