匹配“句号”在mod重写中

发布于 2024-09-10 18:13:18 字数 1247 浏览 1 评论 0原文

目前,我只是匹配 .htaccess 文件中的数字、字母、破折号和下划线:

RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?folder=$1

我还想匹配字符串中的句号。我不想使用:

(.*)

我已经尝试过:

([.A-Za-z0-9-_]+)
([\.A-Za-z0-9-_]+)
([\\.A-Za-z0-9-_]+)
([A-Za-z0-9-_\.]+)

似乎都不起作用......我怎样才能逃脱句号,使其与句号匹配!

---------- 其他信息 ----------------

例如:

mydomain.com/groups/green/ 应该转到index.php?folder=green

此外,我还在上面重写了子域(我认为这导致了复杂化)...

anotherdomain.com 应该映射到 index.php?folder=anotherdomain.com

我已经成功地重写了子域以下规则:

# external group domain name
RewriteCond %{ENV:Rewrite-Done} !^Yes$
## exclude requests from myhost.com
RewriteCond %{HTTP_HOST} !^www\.myhost\.com
## allowed list of domain masking domains
RewriteCond %{HTTP_HOST} ^(anotherdomain.com|extra.domain.com|external.otherdomain.com)
RewriteRule (.*) /groups/%1/$1

我认为这就是复杂性所在。

---------------- 解决方案 ----------------------

尽管没有找到上述问题的具体解决方案,我通过将第一个重定向(映射外部域)从: 更改

 RewriteRule (.*) /groups/%1/$1

为:

RewriteRule (.*) /groups/external/$1&external_domain=%1

第二个重写(在文件夹上)可以解释“外部域”变量而不是文件夹来解决这个问题。

At the moment I am just matching numbers, letters, dashes and underscores in my .htaccess file:

RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?folder=$1

I also want to match full stops in the string. I don't want to use:

(.*)

I have tried:

([.A-Za-z0-9-_]+)
([\.A-Za-z0-9-_]+)
([\\.A-Za-z0-9-_]+)
([A-Za-z0-9-_\.]+)

None of which seem to work.... how can I escape the full stop so it matches a full stop!

---------- Additional information ----------------

As an example:

mydomain.com/groups/green/ should go to index.php?folder=green

In addition I am also re-writing subdomains over the top of this (I think this is causing the complication)...

anotherdomain.com should map to index.php?folder=anotherdomain.com

I have succesfully re-written the subdomain with the following rule:

# external group domain name
RewriteCond %{ENV:Rewrite-Done} !^Yes$
## exclude requests from myhost.com
RewriteCond %{HTTP_HOST} !^www\.myhost\.com
## allowed list of domain masking domains
RewriteCond %{HTTP_HOST} ^(anotherdomain.com|extra.domain.com|external.otherdomain.com)
RewriteRule (.*) /groups/%1/$1

I think this is where the complication lies.

---------------- Solution ----------------------

Despite not finding a solution to the exact problem above, I have worked around it by changing the first re-direct (which maps the external domains) from:

 RewriteRule (.*) /groups/%1/$1

to:

RewriteRule (.*) /groups/external/$1&external_domain=%1

The second re-write (on the folder) can then interpret the "external domain" variable instead of the folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-09-17 18:13:18

您的第一个选项是最简单且正确的。方括号内 . 没有特殊含义,因此您可以逐字包含它,无需任何特殊转义。

实际上0-9-_中的第二个破折号有一个小问题。如果您想在方括号内添加破折号,则应将其放在字符类的开头。否则,它将具有定义字符范围的特殊含义:

([-.A-Za-z0-9_]+)

如果这不起作用,则说明您的 RewriteRule 存在其他问题。例如,如果这是全局规则而不是每个目录(无 RewriteBase),则 URL 将以斜杠 / 开头。

Your first option is the simplest and is correct. Inside square brackets . has no special meaning, so you include it verbatim without any special escaping needed.

Actually there is a small problem with the second dash in 0-9-_. If you want a dash inside square brackets you should place it at the beginning of the character class. Otherwise it will have its special meaning of defining a character range:

([-.A-Za-z0-9_]+)

If that doesn't work there is something else wrong with your RewriteRule. For instance, if this is a global rule rather than per-directory (no RewriteBase) then URLs will begin with a slash /.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文