How can I get a regular expression to only match an empty string? (In a .htaccess file)
32,772
I wasn't convinced this would work, but I've tested it and it does seem to.
Quite simply:
^.{0}$
Where:
^
is the start of the match
.
is any character (except newline)
{0}
is precisely zero times
$
is the end of the match
Related videos on Youtube
Author by
MadsHaupt
Updated on September 18, 2022Comments
-
MadsHaupt over 1 year
I try to make a regular expression in a .htaccess file, that matches only an empty string.
I have tried many things, but it seems like it’s impossible. For example, I tried
^$
, but it's looking for""
that will always exist in a string.So I seek answers to it all possible.
If possible, I would like to hear how to extend such a regular expression together.
Here is the content of my .htaccess file:
RewriteEngine On RewriteRule ^$ https://stald-mariendal.dk/index_tekst [R=302,L] RewriteRule ^guestbook.html$ https://stald-mariendal.dk/gaestebog [R=301,L,NE] RewriteRule ^sites/guestbook.html$ https://stald-mariendal.dk/gaestebog [R=301,L,NE] RewriteRule ^guestbook$ https://stald-mariendal.dk/gaestebog [R=301,L,NE] RewriteRule ^(\w+).html$ https://stald-mariendal.dk/$1 [R=301,L,NE] RewriteRule ^sites/(\w+).html$ https://stald-mariendal.dk/$1 [R=301,L,NE] RewriteCond %{HTTPS} on RewriteRule ^(\w+)$ ?site=$1.html [L] RewriteCond %{HTTPS}| off RewriteRule (.*) https://stald-mariendal.dk%{REQUEST_URI} [R=301,L,NE]
-
MrWhite about 9 years"simply" - There is no need for this additional complexity.
^.{0}$
is the same as simply^$
. The fact that the OP appears to suggest that^$
does not to work implies there is something else amiss. -
Rounin - Standing with Ukraine about 9 yearsAha. I had (mis)understood that
^$
meant the same as^[.\n]*$
- ie. match absolutely any character (including newline), any number of times. I'll take it from your comment above that it actually means match absolutely zero characters. Thanks for the correction. -
MrWhite about 9 yearsYes,
^
and$
are anchors for the start and end of the string. So when placed together there cannot be anything in between. In fact, this can be further simplified to just^
or$
(the former is more common) for a zero-length match. -
MadsHaupt about 9 yearsI hear that there clearly are divided on what ^ and $ do and what it means when they are together.
-
MadsHaupt about 9 yearsSo I am looking for expert knowledge on what ^ and $ means and what it means when you put ^ at the beginning of the regular expression and $ at the end of the regular expression.
-
Martijn almost 9 yearsI've tried the
^$
, this doesn't work in all environments, the^.{0}$
does (too bad I cant remember where environment didnt work) -
Yailen almost 4 yearsI've tried this ^$ and It works, I use this expresion for a Wechall excercise, and the answer was /^$/