.htaccess 中单个 RewriteCond 的多个 RewriteRules [英] Multiple RewriteRules for single RewriteCond in .htaccess

查看:27
本文介绍了.htaccess 中单个 RewriteCond 的多个 RewriteRules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 .htaccess 中有以下命令

I have following command in my .htaccess

    RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
    RewriteRule ^(.*?)-([a-z]+) %2/$1.$2 [L]
    RewriteRule ^(.*?)-([0-9]+)([a-z]) %2/$1$3.$2 [L]

%2 在第二行和后面的行中不起作用.我可以为 %2 定义任何变量并在所有 RewriteRule 命令中使用它吗?以下命令有效

%2 is not working in second and later lines. Can I define any variable for %2 and use it in all RewriteRule commands? Following command works

     RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
     RewriteRule ^(.*?)-([a-z]+) %2/$1.$2 [L]
     RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
     RewriteRule ^(.*?)-([0-9]+)([a-z]) %2/$1$3.$2 [L]

但我想在不重复条件的情况下将 %2 用于多个规则行.

But I want use %2 for multiple rule line without duplicating condition.

推荐答案

该变量必须保存为 Apache var,然后才能使用,无需重复条件.

The variable must be saved as an Apache var, then that can be used without repeated conditions.

保存在 Apache 变量中显示在第二行.在第 3 行和第 4 行中使用已保存的变量.

Saving in Apache variables are shown in second line. Usage of saved vars in 3th and 4th lines.

RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule .? - [E=Wa:%1,E=Wb:%2]
RewriteRule ^(.*?)-([a-z]+) %{ENV:Wb}/$1.%{ENV:Wb} [L]
RewriteRule ^(.*?)-([0-9]+)([a-z]) %{ENV:Wb}/$1$3.$2 [L]

这篇关于.htaccess 中单个 RewriteCond 的多个 RewriteRules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆