开发者

rewrite rule in .htaccess: what does it do

开发者 https://www.devze.com 2023-01-16 11:13 出处:网络
What does this line in .htaccess do, and when would I need it? RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

What does this line in .htaccess do, and when would I need it?

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

I'm working with zend and I noticed the .htaccess generated by zend doesn't have this rule, but I've seen a tutorial that has it without explaining why.

RewriteEngine On
RewriteCo开发者_高级运维nd %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]


It's a RewriteRule redirecting every request except requests for those file types (js, CSS, icons, GIF/JPG/PNG graphics) to index.php.

It's so that requests for static resources don't get handled by index.php (which is generally a good thing, because starting a PHP instance is expensive.)

It is however already handled in the code block below. This part:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

Excludes the redirection to index.php for files that physically exist ( -s); symbolic links ( -); and existing directories (-d).

If you use the second block you quote (the one created by Zend), everything's fine.

Reference in the Apache manual


The rule:

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

basically says: "If the request does NOT (!) end ($) with .js/css/ico/giv/jpg/png redirect it to index.php".

You'd need this rule to avoid images/stylesheets and js script requests being passed to index.php.

0

精彩评论

暂无评论...
验证码 换一张
取 消