Last Updated on May 6, 2021 by Amit

htaccess match root directory

In this article I will show you how to match or rewrite root directory path using RewriteRule directive in an htaccess file.

How to match root directory

RewriteRule directive uses a relative path pattern in htaccess Context. You don’t need to use full path as RewriteRule already knows the current directory or the directory the htaccess is located in.
If your htaccess file is located in root folder then to match against the root directory that is / by default, you can use an empty regex pattern something like the following :

RewriteRule ^$ /demo.php [L]

The ^$ in the pattern above matches an empty URI so in the rule above it matches your root directory if the htaccess is located in root.
If your htaccess file is located in a subdirectory say /demo then to match against the subdirectory you will need to use exact same pattern

RewriteRule ^$ /demo.php [L]

The rule above in /demo/. htaccess matches the /demo/ directory . This will rewrite /demo/ to /demo.php .

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.