[英]How to get cookie value in htaccess and add it as the first segment in url?
我有一個名為“ language ”的Cookie,其中包含我的網站將加載的語言代碼。
我如何檢查它是否已設置並將其添加為我的url中的第一段,如果未設置則進行設置。
例如重定向www.example.com改為www.example.com/en/如果cookie沒有設置,如果cookie設置為www.example.com/ar/為“AR”
在.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.