gpt4 book ai didi

performance - 301 重定向 Apache : It is better to do it in httpd. conf 或 .htaccess?

转载 作者:行者123 更新时间:2023-12-03 23:25:16 25 4
gpt4 key购买 nike

我计划调整我网站的 URL 以获得更好的 SEO 和服务器性能是我非常关心的一个问题。

我不想用许多 301 重定向使 .htaccess 文件过载。
在 httpd.conf 而不是 .htaccess 中这样做更好吗?

.htaccess 文件在每个 html 请求中被读取,然而,httpd.conf 文件在启动 apache 时只加载一次。那是对的吗?
如果是这样,它会不会只改进一次服务器性能负载重定向规则?

最佳答案

htaccess 文件会被缓存,除非它被修改(在某种程度上取决于主机操作系统和文件系统),所以它不会为每个请求加载。但是,apache 文档说,与在 htaccess 文件中包含指令相比,服务器/虚拟主机配置中的指令通常更快。

见:http://httpd.apache.org/docs/current/howto/htaccess.html

In general, you should only use .htaccess files when you don't have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things. Likewise, mod_rewrite directives work better, in many respects, in the main server configuration.

.htaccess files should be used in a case where the content providers need to make configuration changes to the server on a per-directory basis, but do not have root access on the server system. In the event that the server administrator is not willing to make frequent configuration changes, it might be desirable to permit individual users to make these changes in .htaccess files for themselves. This is particularly true, for example, in cases where ISPs are hosting multiple user sites on a single machine, and want their users to be able to alter their configuration.

However, in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a section in your main server configuration file.

There are two main reasons to avoid the use of .htaccess files.

The first of these is performance. When AllowOverride is set to allow the use of .htaccess files, httpd will look in every directory for .htaccess files. Thus, permitting .htaccess files causes a performance hit, whether or not you actually even use them! Also, the .htaccess file is loaded every time a document is requested.

Further note that httpd must look for .htaccess files in all higher-level directories, in order to have a full complement of directives that it must apply. (See section on how directives are applied.) Thus, if a file is requested out of a directory /www/htdocs/example, httpd must look for the following files:

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

And so, for each file access out of that directory, there are 4 additional file-system accesses, even if none of those files are present. (Note that this would only be the case if .htaccess files were enabled for /, which is not usually the case.)

In the case of RewriteRule directives, in .htaccess context these regular expressions must be re-compiled with every request to the directory, whereas in main server configuration context they are compiled once and cached. Additionally, the rules themselves are more complicated, as one must work around the restrictions that come with per-directory context and mod_rewrite. Consult the Rewrite Guide for more detail on this subject.

The second consideration is one of security. You are permitting users to modify server configuration, which may result in changes over which you have no control. Carefully consider whether you want to give your users this privilege. Note also that giving users less privileges than they need will lead to additional technical support requests. Make sure you clearly tell your users what level of privileges you have given them. Specifying exactly what you have set AllowOverride to, and pointing them to the relevant documentation, will save yourself a lot of confusion later.

关于performance - 301 重定向 Apache : It is better to do it in httpd. conf 或 .htaccess?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com