.htaccess - 从子目录重定向到根目录,并在环境中记住请求的路径


.htaccess - redirect from subdirectory to root directory and remember in enviroment requested path

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

我添加规则

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

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

我进一步使用规则

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

所以我有语言在现场展示

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

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

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

这将是

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

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

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

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