2

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]
  • 2
    "I tried ^$, but it's looking for "" that will always exist in a string." - What behaviour are you seeing that appears to suggest this? ^ and $ are anchors indicating the start and end of the string respectively, so what you are suggesting is impossible. – MrWhite Apr 11 '15 at 14:08
2

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

| improve this answer | |
  • 6
    "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. – MrWhite Apr 11 '15 at 14:04
  • 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. – Rounin Apr 11 '15 at 14:12
  • 3
    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. – MrWhite Apr 11 '15 at 14:30
  • I hear that there clearly are divided on what ^ and $ do and what it means when they are together. – MadsHaupt Apr 12 '15 at 17:40
  • 3
    I've tried the ^$, this doesn't work in all environments, the ^.{0}$ does (too bad I cant remember where environment didnt work) – Martijn Jun 10 '15 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.