0

There appears to be some historic confusion on existing answers in SO on the exact regex to use to match a request for the root directory. eg: ^/$ , ^$ or ^/?$

See the answers here and here. So, let's clarify it

Question:

What is currently valid, 'for 2018' way to match a request to the root dir?

Single case test (with chrome)

I did run a quick test and it seems (with chrome at least) to always match ^$ regardless of how the url was typed, Including /?test. This last bit makes me think the server config matters too.

RewriteRule ^/$ /index-with-slash.html [L,R=301,NE] <-- no hits
RewriteRule ^$ /index-wo-slash.html [L,R=301,NE] 
DM8
  • 214
  • 2
  • 10
  • 1
    To match against Root dir in `htaccess` context we use `^$` and `^/$` is used on server.config or virtual host context. – Amit Verma Mar 05 '18 at 15:59
  • reply and explain the difference in scope if there is any and you get the answer ;) – DM8 Mar 16 '18 at 20:19

1 Answers1

0

^/?$ will match both cases. No need to reinvent the wheel.

If you are concerned about regex being heavy on the resources, you likely would not be using Apache anyways.