dslf46995 2017-08-07 04:01
浏览 107

如何在htaccess中获取cookie值并将其添加为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"

  • 写回答

1条回答 默认 最新

  • 普通网友 2017-08-07 05:16
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大