匹配“句号”在mod重写中
目前,我只是匹配 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第一个选项是最简单且正确的。方括号内
.
没有特殊含义,因此您可以逐字包含它,无需任何特殊转义。实际上
0-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: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
/
.