Saefely在.htaccess mod_rewrite正则表达式中转义句点(。)字符

时间:2022-11-25 11:18:47

I have .htaccess file that is used by an advanced SEO URL php system installed on my osCommerce site. It has the following rules that work just fine for most cases, but removing periods from my GET parameters:

我有.htaccess文件,由我的osCommerce网站上安装的高级SEO URL php系统使用。它具有以下规则,适用于大多数情况,但从我的GET参数中删除句点:

  RewriteRule ^([a-z0-9/-]+)-c-([0-9_]+).html$ index.php [NC,L,QSA]
  RewriteRule ^([a-z0-9/-]+)-m-([0-9]+).html$ index.php [NC,L,QSA]

So URL like this:

所以像这样的URL:

http://example.com//index.php?cPath=44_95&page=1&range=1.99_2.99

gets rewritten according to the rule and the 1.99_2.99 becomes 199_299

根据规则重写,1.99_2.99变为199_299

How can I escape the period safely? (ie. without causing some random side effects)

我怎样才能安全地逃离这段时期? (即没有引起一些随机副作用)

1 个解决方案

#1


2  

The standard escape character for .htaccess regular expressions is the slash ("\").

.htaccess正则表达式的标准转义字符是斜杠(“\”)。

  RewriteRule ^([a-z0-9/-]+)-c-([0-9_]+)\.html$ index.php [NC,L,QSA]
                                        ^^
  RewriteRule ^([a-z0-9/-]+)-m-([0-9]+)\.html$ index.php [NC,L,QSA]
                                       ^^

The slash will prevent the meaning of the dot and escape it so that the dot is taken verbatim as a character to match (period, ASCII code 46 / x2E) .

斜杠将阻止点的含义并将其转义,以便将点作为要匹配的字符逐字逐句(句点,ASCII代码46 / x2E)。

The other suggestion given in the comment to create a character class consisting of the dot only ("[.]") does the job as well, but it's perhaps a bit over the top to create a character class while you only want to name a single character. But it's technically working (and has been suggested for example in escaping dot in Apache mod_rewrite).

在注释中给出的另一个建议是创建一个仅由点组成的字符类(“[。]”)也可以完成这项工作,但创建一个字符类可能有点过头,而你只想命名一个单个字符。但它在技术上是有效的(并且例如在Apache mod_rewrite中转义点时提出过)。

BTW: Apache rewrite uses Perl Compatible Regular Expression (PCRE) which is the same flavour of regex like PHP is using in the preg_* family of functions which is PHP's preferred regex dialect.

顺便说一句:Apache重写使用Perl兼容正则表达式(PCRE),它与PHP正在使用的preg_ *系列函数中使用的正则表达式相同,这是PHP首选的正则表达式方言。

#1


2  

The standard escape character for .htaccess regular expressions is the slash ("\").

.htaccess正则表达式的标准转义字符是斜杠(“\”)。

  RewriteRule ^([a-z0-9/-]+)-c-([0-9_]+)\.html$ index.php [NC,L,QSA]
                                        ^^
  RewriteRule ^([a-z0-9/-]+)-m-([0-9]+)\.html$ index.php [NC,L,QSA]
                                       ^^

The slash will prevent the meaning of the dot and escape it so that the dot is taken verbatim as a character to match (period, ASCII code 46 / x2E) .

斜杠将阻止点的含义并将其转义,以便将点作为要匹配的字符逐字逐句(句点,ASCII代码46 / x2E)。

The other suggestion given in the comment to create a character class consisting of the dot only ("[.]") does the job as well, but it's perhaps a bit over the top to create a character class while you only want to name a single character. But it's technically working (and has been suggested for example in escaping dot in Apache mod_rewrite).

在注释中给出的另一个建议是创建一个仅由点组成的字符类(“[。]”)也可以完成这项工作,但创建一个字符类可能有点过头,而你只想命名一个单个字符。但它在技术上是有效的(并且例如在Apache mod_rewrite中转义点时提出过)。

BTW: Apache rewrite uses Perl Compatible Regular Expression (PCRE) which is the same flavour of regex like PHP is using in the preg_* family of functions which is PHP's preferred regex dialect.

顺便说一句:Apache重写使用Perl兼容正则表达式(PCRE),它与PHP正在使用的preg_ *系列函数中使用的正则表达式相同,这是PHP首选的正则表达式方言。