.htaccess并在URL中删除文件名

I am currently trying to drop the filename (.php) from the URLs on my site and came across the following .htaccess code:

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

I have tested this and it works correctly on my site. The concern I have is the site will receive 100,000+ hits from all over the world...will this .htaccess rewrite cause any overload to my server?

I was messing with custom URL rewrites in .htaccess a few months back, on another project and it kept overloading the memory on the server.

Is there a way to protect this from happening or is this a non-issue with this type of rewrite in .htaccess?

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>