【问题标题】:.htaccess - redirect from subdirectory to root directory and remember in enviroment requested path.htaccess - 从子目录重定向到根目录并记住环境请求的路径
【发布时间】:2016-06-25 07:15:45
【问题描述】:

我对语言文件夹的 mod_rewrite 规则有一些困难。我想重写任何请求: www.example.com/lang/* 到: www.example.com/*

我添加规则

RewriteRule ^lang/(.*)$ \/$1

由于我使用多种语言,我想保存在环境中,网址中的语言在第一次打开网站时输入。

我还要使用规则

SetEnvIf Referer "www.example.com/lang1/" LANG=1
SetEnvIf Referer "www.example.com/lang2/" LANG=2

所以我有语言可以在网站上显示

但是在浏览器中输入地址 www.example.com/lang1/ 时我无法获得这个环境

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    由于这是内部重写(使用新路径而不是旧路径),与客户端没有通信,因此没有设置新的Referer。

    但是使用referer总是不可靠的。考虑修改 RewriteRule,使其在查询字符串中包含原始请求 URI。

    这将是

    RewriteRule ^lang/(.*)$ \/$1&uri=%{REQUEST_URI}
    

    如果您的请求包含查询字符串,并且

    RewriteRule ^lang/(.*)$ \/$1?uri=%{REQUEST_URI}
    

    如果没有。然后原始 URI 将包含在变量 $_GET['uri'] 中。

    【讨论】:

    • 原始 URI 将不包含在变量 $_SERVER['uri'] 中,而是包含在 $_GET['uri'] 中并且它可以工作。谢谢。
    • 抱歉,输入错误。固定的。感谢您的通知。
    • 如果可行,请考虑接受并可能支持答案。
    • 如果请求可以包含或不包含查询字符串,有没有办法改变? to & in 规则:$1__?/&__uri=
    • 在我的例子中,我使用查询而不是两个条件进行了这样的重定向:RewriteRule ^lang/(.*)$ /$1?uri=%{REQUEST_URI}$2 [P,QSA]
    猜你喜欢
    • 1970-01-01
    • 2010-12-09
    • 2021-04-04
    • 2018-01-23
    • 2016-07-18
    • 2011-02-28
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多