简体   繁体   English

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

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

I have some difficulties with the mod_rewrite rules for language folders. 我对语言文件夹的mod_rewrite规则有一些困难。 I want to rewrite any request on: www.example.com/lang/* to: www.example.com/* 我想将www.example.com/lang/*上的任何请求重写为:www.example.com/*

I add rule 我添加规则

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

As I use several language I'd like to save in enviroment wich exactly language in URL is typed at first opening of site. 当我使用多种语言时,我想保存在环境中,而在网站的第一个网站中输入的正是URL中的语言。

Futher I use rule 我还使用规则

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

So I got language to show on site 所以我在现场展示语言

However I could not get this enviroment when typing address www.example.com/lang1/ in browser 但是,在浏览器中输入地址www.example.com/lang1/时,我无法获得此环境

As this is an internal rewrite (the new path is used instead of the old one), there is no communication with the client and therefore no new Referer set. 由于这是内部重写(使用新路径代替旧路径),因此与客户端之间没有通信,因此也没有新的Referer集。

But using the referer is always unreliable. 但是使用引用程序始终是不可靠的。 Consider modifying the RewriteRule so that it contains the original request URI in the query string. 考虑修改RewriteRule,使其在查询字符串中包含原始请求URI。

This would be 这将是

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

if your requests contain a query string, and 如果您的请求包含查询字符串,并且

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

if not. 如果不。 The original URI would then be contained in the variable $_GET['uri'] . 然后,原始URI将包含在变量$_GET['uri']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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