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

Share:
32,772

Related videos on Youtube

MadsHaupt
Author by

MadsHaupt

Updated on September 18, 2022

Comments

  • MadsHaupt
    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
    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
    Rounin - Standing with Ukraine about 9 years
    Aha. 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
    MrWhite about 9 years
    Yes, ^ 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
    MadsHaupt about 9 years
    I hear that there clearly are divided on what ^ and $ do and what it means when they are together.
  • MadsHaupt
    MadsHaupt about 9 years
    So 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
    Martijn almost 9 years
    I've tried the ^$, this doesn't work in all environments, the ^.{0}$ does (too bad I cant remember where environment didnt work)
  • Yailen
    Yailen almost 4 years
    I've tried this ^$ and It works, I use this expresion for a Wechall excercise, and the answer was /^$/