Apache-2.2

Apache AddType 和 AddHandler 指令之間的區別?

  • April 1, 2021

我的理解是:

  • AddHandler- 用於伺服器
  • AddType- 對於客戶端(瀏覽器)
AddType application/x-httpd-php4 .php4
AddHandler application/x-httpd-php4 .php4

但為什麼我們需要同時添加兩者?在 PHP 的情況下兩者都需要嗎?AddTypePHP需要嗎?

  • AddType:將給定的文件副檔名映射到指定的內容類型
  • AddHandler:將文件副檔名映射到指定的處理程序

在您的情況下,內容類型等於處理程序的名稱只是巧合 - 假設實際上有一個名為application/x-httpd-php4. 否則就大錯特錯了。

如前所述:

  1. AddType用於將文件副檔名與 MIME 類型相關聯(因此更改content-typeHTTP 響應標頭)
  2. AddHandler/SetHandler用於將文件副檔名綁定到伺服器端處理程序(如 PHP)。

似乎使用綁定 PHP 處理程序是一種在 Apache 1.1.0 中引入/AddType之前使用的過時方式。AddHandler``SetHandler

我能找到的一點歷史

Apache 1.0.x中沒有AddHandler/SetHandler

https://svn.apache.org/repos/asf/httpd/httpd/tags/1.3/apache_1_0_0/src/modules/standard/mod_mime.c包含:

command_rec mime_cmds[] = {
{ "AddType", add_type, NULL, OR_FILEINFO, ITERATE2,
   "a mime type followed by one or more file extensions" },
{ "AddEncoding", add_encoding, NULL, OR_FILEINFO, ITERATE2,
   "an encoding (e.g., gzip), followed by one or more file extensions" },
{ "AddLanguage", add_language, NULL, OR_FILEINFO, ITERATE2,
   "a language (e.g., fr), followed by one or more file extensions" },
{ "TypesConfig", set_types_config, NULL, RSRC_CONF, TAKE1,
   "the MIME types config file" },
{ NULL }
};

1996 年 4 月

PHP/FI (PHP 2.0)引入了PHP 的Apache 模組版本。當時的官方 Apache 版本是 1.0.5。

來自https://museum.php.net/php2/php-1.99s.tar.gz的自述文件包含:

Step 4. (If you are NOT installing the Apache module version)
 
 Copy the php.cgi binary to your system's cgi-bin directory.  If you
 do not have access to do this and wish to install it in your own
 personal directory, you may do so, but you should set the setuid
 bit on the executable with: chmod u+s /path/php.cgi
 Setting the setuid bit is not crucial.  The benefit is that any files
 created by php will be owned by you.  This means that you can edit
 and delete such files directly.  Without the setuid bit set these
 files will be owned by the httpd user id.

Step 4. (if you are installing the Apache module version)

 Change to your Apache src directory where the mod_php.c and mod_php.h
 files should have been copied to. If they weren't which usually happens
 because of permission problems, copy these two files there manually. Edit
 your Apache Configuration file and add the EXTRA_LIBS line which was
 produced at the end of Step 3. And add:

   Module php_module mod_php.o

 to the very end of the file. Then type: ./Configure and then make to rebuild
 your Apache httpd binary. Install this binary.

 Next you need to edit your Apache conf/srm.conf file and add a line like:

   AddType application/x-httpd-php .phtml

 This defines a new MIME, application/x-httpd-php, which will trigger the
 PHP module to parse any file ending with the .phtml extension. You can pick
 any extension you like for this.

 Now you are ready to restar your httpd server. See the Apache Module
 Notes for more details on configuring the PHP Module.

來自https://museum.php.net/php2/php-1.99s.tar.gz的src/mod_php.c包含:

handler_rec php_handlers[] = {
       { "application/x-httpd-php", send_parsed_php },
       { NULL }
};

module php_module = {
       STANDARD_MODULE_STUFF,
       NULL,                           /* initializer */
       php_create_conf,        /* dir config creater */
       NULL,                           /* dir merger --- default is to override */
       NULL,                           /* server config */
       NULL,                           /* merge server config */
       php_commands,           /* command table */
       php_handlers,           /* handlers */
       NULL,                           /* filename translation */
       NULL,                           /* check_user_id */
       NULL,                           /* check auth */
       NULL,                           /* check access */
       NULL,                           /* type_checker */
       NULL,                           /* fixups */
       NULL                            /* logger */
};

1996 年 7 月

AddHandler/是在 Apache 1.1.0SetHandler 中引入的。

https://svn.apache.org/repos/asf/httpd/httpd/tags/1.3/APACHE_1_1_0/src/CHANGES包含:

 *) Add AddHandler command, which allows content-type-independent
    "handlers" to be defined for file extensions. These can be either
    built into Apache (such as CGI scripts or server-side includes, or
    added with the Action command). [Alexei Kosut]

https://svn.apache.org/repos/asf/httpd/httpd/tags/1.3/APACHE_1_1_0/src/modules/standard/mod_mime.c包含:

command_rec mime_cmds[] = {
{ "AddType", add_type, NULL, OR_FILEINFO, ITERATE2,
   "a mime type followed by one or more file extensions" },
{ "AddEncoding", add_encoding, NULL, OR_FILEINFO, ITERATE2,
   "an encoding (e.g., gzip), followed by one or more file extensions" },
{ "AddLanguage", add_language, NULL, OR_FILEINFO, ITERATE2,
   "a language (e.g., fr), followed by one or more file extensions" },
{ "AddHandler", add_handler, NULL, OR_FILEINFO, ITERATE2,
   "a handler name followed by one or more file extensions" },
{ "ForceType", set_string_slot, (void*)XtOffsetOf(mime_dir_config, type),
   OR_FILEINFO, TAKE1, "a media type" },
{ "SetHandler", set_string_slot, (void*)XtOffsetOf(mime_dir_config, handler),
   OR_FILEINFO, TAKE1, "a handler name" },
{ "TypesConfig", set_types_config, NULL, RSRC_CONF, TAKE1,
   "the MIME types config file" },
{ NULL }
};

2004 年 12 月

Rasmus Lerdorf 說

就像大多數沿著這些構想的批評一樣,他們並不費心去研究為什麼某事是某種方式。曾經有一段時間沒有 AddHandler 指令。Addtype 是您執行此操作的方式。由於 AddType 適用於每個版本的 Apache,即使在添加 AddHandler 之後,我也從來沒有看到過更改它的理由,因為它本質上是純粹的學術性的。

另請參閱https://bugs.php.net/bug.php?id=36772

2008 年 6 月

AddType 出於安全原因,在 Apache 2.x的PHP 安裝指南中已替換SetHandler/ 。AddHandler

另請參閱https://www.php.net/manual/en/install.unix.apache2.php#92797

PS:但是Apache 1.3.x 的 PHP 安裝指南仍然包含AddType(而且它似乎也過時了)。

引用自:https://serverfault.com/questions/131206