繁体   English   中英

.htaccess基于cookie重写到目录

[英].htaccess rewrite to directory based on cookie

在我的应用程序中,我想使用mod_rewrite来拥有漂亮的URL,而且还要将登录用户(通过logged cookie检测)重定向到旧的不太好的URL,如下所示:

不错的网址: mydomain.com/topic/12-name/

网址不太好: mydomain.com/other_topic?item_id=12&other=params

我的.htaccess

RewriteEngine on

RewriteCond %{HTTP_COOKIE} logged
RewriteRule ^topic/([0-9]+)-([a-zA-Z0-9_-]+)/$ /other_topic?item_id=$1&other=params [R=301,L]

RewriteRule ^topic/([0-9]+)-([a-zA-Z0-9_-]+)/$ /other_topic?item_id=$1&other=params [NC,L]

第一个-重定向-效果很好。 第二个-重写-根本不起作用。 有什么想法吗?

未经测试,但这应该做您想要的。 请注意,没有重定向代码的R标志默认为临时302重定向。 如果您更清楚,也可以输入R=302 ,我更喜欢R因为我很容易记住,当记住302是临时的而301是永久的时,它默认为临时重定向对我来说更难。 尽管需要查找主题的“名称”,但从PHP进行这种重定向可能更有意义。

RewriteEngine on

#First let's match logged in users and make sure they're visiting the ugly url
RewriteCond %{HTTP_COOKIE} logged
RewriteRule ^topic/([0-9]+)-([a-zA-Z0-9_-]+)/$ /other_topic?item_id=$1&other=params [R,L]

#Next let's match guests and make sure they're visiting the clean url
RewriteCond %{HTTP_COOKIE} !logged
RewriteCond %{QUERY_STRING} (?:^|&)item_id=(.*?)(?:$|&)
RewriteCond %{QUERY_STRING} (?:^|&)other=.*?(?:$|&)
RewriteRule ^/?other_topic/?$ /topic/%1-name/? [R,L]

#If we're still here, let's rewrite clean urls to our actual file
RewriteRule ^topic/([0-9]+)-([a-zA-Z0-9_-]+)/$ /other_topic?item_id=$1&other=params [NC,L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM