简体   繁体   English

这个正则表达式如何在htaccess中匹配

[英]How this regex matches in htaccess

I am trying to add trailing slash to a url. 我正在尝试在网址中添加斜杠。 I have my own logic to do so but found another one on stackoverflow ( here ). 我有自己的逻辑,但是在stackoverflow上找到了另一个( 此处 )。 Now the regex in this line 现在这行的正则表达式

RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

apparently matches the RewriteRule for the url http://www.example.com/wp-admin . 显然与网址http://www.example.com/wp-admin的RewriteRule匹配。

What I first did was: 我首先要做的是:

RewriteCond %{REQUEST_URI} /wp-admin$ [NC]
RewriteCond %{REQUEST_URI} !/+$
RewriteRule ^ %{ENV:proto}://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

So my question is how /wp-admin$ is similar to ^([_0-9a-zA-Z-]+/)?wp-admin$ ? 所以我的问题是/wp-admin$^([_0-9a-zA-Z-]+/)?wp-admin$相似^([_0-9a-zA-Z-]+/)?wp-admin$

This is because leading slash is matched when you use RewriteCond %{REQUEST_URI} but current directory (relative to DocumentRoot ) is stripped when you use a pattern in RewriteRule in .htaccess file, which is a per directory directive. 这是因为当您使用RewriteCond %{REQUEST_URI}时,前导斜杠是匹配的,但是当您在.htaccess文件的RewriteRule中使用模式(每个目录指令)时,当前目录(相对于DocumentRoot )将被剥离。

Hence a leading slash is not matched in RewriteRule but is matched in RewriteCond . 因此,前斜杠在RewriteRule不匹配,但在RewriteCond匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM