开发者

Rewrite rule with the exception of static content

开发者 https://www.devze.com 2022-12-18 15:18 出处:网络
My 开发者_StackOverflow中文版current htaccess file looks like this: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f [NC]

My 开发者_StackOverflow中文版current htaccess file looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

This works fine for me, however I want to try and reduce server load so I have added this line just below the RewriteEngine On line to stop processing if loading static content:

RewriteCond %{REQUEST_FILENAME} !.*jpg$|.*gif$|.*png$|.*css$|.*js$|.*txt$|.*ico$|.*pdf$ [NC]

Is this the most efficient way of writing it? Thanks


In line 2 you already check for existence of the requested file (!-f), so there is no need to add your line, but you could replace line 2 with your new rewrite condition, if you want to avoid returning other files like .php files. Performancewise I do not think this will speed up apache though, and there is some other performance hole in your web application.

That said, your pattern is probably not what you expected to be, as e.g. .*jpg$ matches mysuperjpg as well. You may be looking for \.jpg$, because . is a special character for regular expressions. You could do this:

RewriteCond %{REQUEST_FILENAME} !\.(jpg|gif|png|css|js|txt|ico|pdf)$ [NC]
0

精彩评论

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

关注公众号