.htaccess - 如何将 URL 参数存储在 htaccess 变量中

标签 .htaccess

我所有的 URL 都可以附加 ?lang=xx 但我不想重写我所有的 htaccess 规则,所以我想捕获这个参数并可用时附上。

我试图将参数保存到变量 MYLANG 如下,但不起作用:

SetEnv MYLANG %{QUERY_STRING} ^lang=([a-zA-Z]{2})$

这是另一个失败的尝试:

RewriteCond %{QUERY_STRING} ^lang=([a-zA-Z]{2})$
SetEnv MYLANG %1

又一次尝试:

RewriteCond %{QUERY_STRING} ^lang=([a-zA-Z]{2})$
RewriteRule ^ - [env=MYLANG:%1]

然后我可以在规则中使用捕获的变量:

RewriteRule ^(.*).html$ /?name=$1&lang=%{ENV:MYLANG} [NC,L]

或者

RewriteRule ^(.*).html$ /?name=$1 [NC,L,E=MYLANG]

最佳答案

我从未使用过它,但在我看来就是这样:

RewriteCond %{QUERY_STRING} ^lang=([a-zA-Z]{2})$
RewriteRule ^ - [E=MYLANG:%1]

RewriteRule ^(.*).html$ /?name=$1&lang=%{ENV:MYLANG} [NC,L]

关于.htaccess - 如何将 URL 参数存储在 htaccess 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41863278/

相关文章:

regex - Windows 本地主机的 .htaccess 文件配置

regex - 将 .htaccess 中的大写字母转换为小写

regex - [领域]/?使用 .htaccess 重定向到 [域]

.htaccess - Access-Control-Allow-Origin htaccess 文件不起作用

wordpress - 使用 .htaccess 将国家/地区后缀添加到域

php - 如何为 Laravel 发行和定制 .htaccess?

php - htaccess 重定向到父文件夹 index.php 如果不存在

apache - .htaccess 文件找不到 404 ErrorDocument

php - mod_rewrite 与永久重定向

AngularJS 导航到 Apache 中的路线时给出 404