1

我目前正在尝试从我网站上的 URL 中删除文件名 (.php),并遇到了以下 .htaccess 代码:

# Remove filename extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

我已经对此进行了测试,它在我的网站上可以正常工作。我担心的是该站点将收到来自世界各地的 100,000 多次点击……这个 .htaccess 重写会导致我的服务器过载吗?

几个月前,在另一个项目中,我在 .htaccess 中搞乱了自定义 URL 重写,它一直使服务器上的内存超载。

有没有办法防止这种情况发生,或者在.htaccess 中这种类型的重写是否没有问题?

4

1 回答 1

1

Read up on "greedy" and "lazy" Regular Expression. The First segment of your RewriteRule is lazy (which is better than greedy); however it is still refined enough to know exactly what it's looking for.

Greedy would require a significant amount of memory. You might want to make it [QSA,NC,L].

QSA will add any ?query=strings, the NC forces the url to ignore the case, and L means it's the last rewrite rule to check until the next rewrite condition.


Wrapping it in a IFModule is pretty important, don't want server 500 errors if Rewrite isn't working right. The RewriteBase will tell it to get it's sought after files from the root of the folder the .htaccess file is sitting in.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^\.]+)$ $1.php [QSA,NC,L]
</IfModule>
于 2013-10-01T02:38:43.150 回答