简体   繁体   中英

How to get cookie value in htaccess and add it as the first segment in url?

I have a Cookie named " language " which has the language code which my website would load in .

How can i check if it's set and add it as the first segment in my url , and set it if it's not set .

For example redirect www.example.com to www.example.com/en/ if the cookie is not set and to www.example.com/ar/ if the cookie is set to " ar "

In .htaccess, you can check whether a specific cookie value exists using a RewriteCond and %{HTTP_COOKIE} variable.

To redirect based on the cookie value, you can use something like the following :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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