.htaccess redirect performance

北慕城南 提交于 2019-12-01 14:30:53

问题


Are htaccess redirects any slower/put any more strain on Apache than redirects in configuration files? Also, how big can a number of redirects in htaccess get before they start decreasing performance?


回答1:


Yes, it slows the server because it has to access the file each time a resource in that directory or any subdirectory thereof is accessed.

The amount of redirects is not relevant, because the main performance hit is the file access itself. This within reasonable constraints (ie a 5 Kb htaccess file will take more or less the same time to be parsed than a 1 Kb one, different story is a 1Mb htaccess, though I've never seen that monstrosity and hope I never will)




回答2:


While it's true that the .htaccess is parsed on each request, and is thus technically slower than putting your rules in the main config file, in reality it doesn't matter. The apache configuration engine is fairly optmized C code that's embedded in the web server. Unless you're only serving small static files with no database accesses at all, the extra overhead of .htaccess and redirects is negligible.

Modern processors are so fast that you'd really have to be doing a massive amount of traffic to worry about this. If you're doing this much traffic, and since it's all static content, go ahead and buy yourself a second server to share the load.




回答3:


Using a .htaccess file is slower than using a configuration file - a .htaccess file is parsed whenever a request is made to a directory it affects - this allows for changing the file without restarting the server. Since a configuration file is parsed only once at server start, it's faster.

The amount of directives you can have in a .htaccess file without significant performance impact will be based on the complexity of the rules and your server's specifics, although the main performance hit will be from using the .htaccess file at all.



来源:https://stackoverflow.com/questions/209209/htaccess-redirect-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!