如何在htaccess中获取cookie值并将其添加为url中的第一个段?

时间:2017-08-07 04:01:30

标签: php .htaccess cookies

我的Cookie名为" 语言"其中包含我的网站将加载的语言代码。

我如何检查它是否已设置并将其添加为我的网址中的第一个细分,如果未设置则设置它。

例如,如果未设置Cookie,则将 www.example.com 重定向至 www.example.com/en / www.example.com / ar / 如果Cookie设置为" ar "

1 个答案:

答案 0 :(得分:0)

在.htaccess中,您可以使用 RewriteCond %{HTTP_COOKIE} 变量检查是否存在特定的Cookie值。

要根据Cookie值重定向,您可以使用以下内容:

RewriteEngine on
#if cookie lang is not set, 
RewriteCond %{HTTP_COOKIE} ^$
RewriteRule ^$ http://example.com/en [L,R]
 #if the cookie lang  is set to "fr"  we will redirect to append the value at the end of the destination url
RewriteCond %{HTTP_COOKIE} ^lang=(fr)$
RewriteRule ^$ http://example.com/%1 [L,R]
Rewrite