Ven Nilson
Ven Nilson

Reputation: 1019

What exactly this rewrite condition does?

I'm facing a problem to understand this mod_rewrite expression. I'm using local xampp environment which has a directory named root in the htdocs directory. When I enter in address bar like this localhost/root it opens up the index.php page. This is the code of .htaccess file.

RewriteEngine on
RewriteBase /root/
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^.*$ index.php [NC,L]

I'm facing a problem on line no 3, I know RewriteRule will only work when condition becomes true. But what is the purpose of this condition. What it does in this case.

Upvotes: 0

Views: 81

Answers (1)

Daan Meijer
Daan Meijer

Reputation: 1348

The purpose of this rule is to NOT match on any static files (defined as ending on .css, .js, .png, .jpg, .gif or robots.txt, case insensitive). Any request matching the rule (i.e. not ending on any of the specified strings) will get passed to index.php.

For example: if a request is for /images/logo.png, it ends with .png which means it DOES satisfy the condition between the parentheses, and therefore DOES NOT satisfy the entire expression. So, it will not get passed to index.php. However, a request for /blog/hello-world/ DOES NOT end with one of the mentioned strings, therefore it DOES satisfy the entire expression. Hence it will get handled by index.php.

Upvotes: 1

Related Questions