douxing8939 2014-01-28 13:24
浏览 15
已采纳

.htaccess重写与cookie drop不一致

I have in my .htaccess file the following code:

RewriteRule ^(en|af|fr)/(.*)$ - [co=lang:$1:mysite.info:7200:/] #drop/update cookie with lang
RewriteRule ^(en|af|fr)$ - [co=lang:$1:mysite.info:7200:/] #drop/update cookie with lang
RewriteRule ^(en|af|fr)$    / [R,NC]
RewriteRule ^(en|af|fr)/(.*)$ /$2 [R,NC]

In my php I have this for selecting the language:

<?php echo $_COOKIE['lang']; ?>
<ul>
    <li><a href="/en<? echo $_SERVER['REQUEST_URI'];?>">en-ZA</a></li>
    <li><a href="/fr<? echo $_SERVER['REQUEST_URI'];?>">fr-MU</a></li>
    <li><a href="/af<? echo $_SERVER['REQUEST_URI'];?>">af-ZA</a></li>
</ul>

It semi-works, but it's not consistent. While I'm switching between languages it simply stops working, and then starts again if I keep clicking on the different languages.

SECOND PART of question:

How to reduce these lines:

RewriteCond %{http_host} ^www.mysite.info [nc] #if url starts with www.mysite.info
RewriteRule ^(.*)$ http://mysite.info/$1 [r,nc] #rewrite without the www

RewriteCond %{http_host} ^www.(.*).mysite.info [nc] #if url starts with www.(aff).mysite.info
RewriteRule ^(.*)$ http://%1.mysite.info/$1 [r,nc] #rewrite without the www

RewriteCond %{HTTP_HOST} !^www\.  [NC]
RewriteCond %{http_host} ^(.*)\.mysite.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ - [co=aff:%1:mysite.info:7200:/] #drop/update cookie with aff

RewriteCond %{HTTP_HOST} !^www\.  [NC]
RewriteCond %{http_host} ^(.*)\.mysite.info [nc] #if subdomain is aff
RewriteRule ^(.*)$ http://mysite.info/$1 [nc,R] #rewrite without subdomain

Appreciate the help, anubhava!

  • 写回答

1条回答 默认 最新

  • dongza3124 2014-01-28 13:31
    关注

    Not sure you have repeated rules in there. Try this rule to replace all 4 of your rules:

    RewriteRule ^(en|af|fr)(/.*)?$ /$2 [co=lang:$1:mysite.info:7200:/,NC,L,R]
    

    UPDATE:

    RewriteCond %{http_host} ^([^.]+)\.mysite\.info$ [NC]
    RewriteRule ^(.*)$ http://mysite.info/$1 [L,R]
    
    RewriteCond %{http_host} ^www.([^.]+)\.mysite\.info$ [NC]
    RewriteRule ^(.*)$ http://%1.mysite.info/$1 [L,R,NC]
    
    RewriteCond %{HTTP_HOST} !^www\.  [NC]
    RewriteCond %{http_host} ^([^.]+)\.mysite.info [NC]
    RewriteRule ^(.*)$ - [co=aff:%1:mysite.info:7200:/]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄