Mobile app version of vmapp.org
Login or Join
Courtney195

: REGEX with RewriteRule This regex is confusing. I want to know its exact meaning. I try to explain by myself. I hope I won't get an error understanding. Please kindly correct my thoughts.

@Courtney195

Posted in: #Regex

This regex is confusing. I want to know its exact meaning. I try to explain by myself. I hope I won't get an error understanding. Please kindly correct my thoughts.


RewriteRule "(^|/).(?!well-known)" - [F]
RewriteRule "/.|^.(?!well-known/)" - [F]


About (^|/).(?!well-known), I think the right example is:


.ohya
.webserver/ohya
/.ohya
/webserver/ohya ==> Isn't it just like this /.webserver/ohya as a proper example?


Maybe there are lots of derivative examples but I just raise these four.

The other one, /.|^.(?!ohya/)==> (Isn't it like this /.|^.(?!well-known/) as for the proper explanation as below? Or this was my typo possibly.) , is as:


/.ohya/
/.webserver/ohya/
.ohya/
./webserver/ohya/


!well-known is except of well-known this string. ?!well-known is with or without any previous string such as an directory.

I found a different between their directory path. The above one is without directory. Are there anything else? I hope I don't make a wrong recognition on REGEX.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Courtney195

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

Yes, I think your understanding of what this regex pattern matches is pretty much correct, except for /webserver/ohya - this will not match the first pattern since there is not dot prefix. And whilst ./webserver/ohya/ would technically match the second pattern, this is not a real world URL-path.


Maybe there are lots of derivative examples


Yes, there are pretty much an infinite number of potential matches.

But note that if the pattern matches then access is blocked (403 Forbidden).

Note the dots are backslash escaped in the regex in order to match literal dots. (The backslashes were not showing in your initial question until I edited it. You appear to have copied/quoted the version without backslashes - this changes the meaning somewhat, although you are describing the escaped version.)

What does this RewriteRule do?

It blocks access to all dot-files (files starting with a dot), except for .well-known, which is often used to validate SSL cert installation (eg. "Let's Encrypt").



RewriteRule "(^|/).(?!well-known)" - [F]




(^|/) - (alternation) Matches either the start of the URL-path or a slash
. - Matches a literal dot
(?!well-known) (negative lookahead) - Successful when it doesn't match the string "well-known".


So this matches either . (at the start of the URL-path) or /. (anywhere) that is not followed by the string "well-known".



RewriteRule "/.|^.(?!well-known/)" - [F]




In this case the alternation covers the entire regex, so you have either /. or ^.(?!well-known/)


This is similar to #1 , however, it will match a string of the form /.well-known, whereas the first will not.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme