使用 .htaccess 使所有 .html 页面作为 .php 文件运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4687208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:58:44  来源:igfitidea点击:

Using .htaccess to make all .html pages to run as .php files?

phphtmlapache.htaccess

提问by Michael Novello

I need to run all of my .html files as .php files and I don't have time to change all of the links before our presentation tomorrow. Is there any way to "hack" this with my Apache server?

我需要将我所有的 .html 文件作为 .php 文件运行,而且我没有时间在明天的演示之前更改所有链接。有什么办法可以用我的 Apache 服务器“破解”它吗?

回答by Marc-Fran?ois

Create a .htaccess file at the root of your website and add this line:

在您网站的根目录创建一个 .htaccess 文件并添加以下行:

[Apache2 @ Ubuntu/Debian: use this directive]

[Apache2 @ Ubuntu/Debian:使用这个指令]

AddType application/x-httpd-php .html .htm

Or, from comment below:

或者,从下面的评论:

AddType application/x-httpd-php5 .html .htm

If your are running PHP as CGI (probably not the case), you should write instead:

如果您将 PHP 作为 CGI 运行(可能不是这种情况),您应该改为编写:

AddHandler application/x-httpd-php .html .htm 

回答by tony

In My Godaddy Server the following code worked

在我的 Godaddy 服务器中,以下代码有效

Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html

回答by starkeen

You may also use the H or T flag of mod_rewrite to force all .html files to be parsed by php handler :

您还可以使用 mod_rewrite 的 H 或 T 标志来强制所有 .html 文件由 php 处理程序解析:

using H (Handler) flag:

使用 H(处理程序)标志:

 RewriteEngine on

RewriteRule \.(html|htm)$ - [H=application/x-httpd-php5]

using T (Type) flag :

使用 T(类型)标志:

 RewriteEngine on

RewriteRule \.(html|htm)$ - [T=application/x-httpd-php5]

Or you can add more extensions to the rule pattern seprated by a pipe |that you want to be parsed by php handler

或者您可以向由管道分隔的规则模式添加更多扩展| 您希望由 php 处理程序解析

ex :

前任 :

RewriteRule \.(html|htm|txt|foo)$ - [T=application/x-httpd-php5]

the example above will change the mime type of files that end with .html, .htm, .txt, .footo php.

上面的示例将以.html.htm.txt.foo结尾的文件的 mime 类型更改为 php。

Note : on some servers you will have to change php5to phpto get this example to work in handler string:

注意:在某些服务器上,您必须将php5更改为php才能使此示例在处理程序字符串中工作:

Change it

更改

[T=application/x-httpd-php5]

to

[T=application/x-httpd-php]

回答by Dorad

You need to add the following line into your Apache config file:

您需要将以下行添加到您的 Apache 配置文件中:

AddType application/x-httpd-php .htm .html

You also need two other things:

您还需要另外两件事:

  1. Allow Overridding

    In your_site.conffile (e.g. under /etc/apache2/mods-availablein my case), add the following lines:

    <Directory "<path_to_your_html_dir(in my case: /var/www/html)>">
        AllowOverride All
    </Directory>
    
  2. Enable Rewrite Mod

    Run this command on your machine:

    sudo a2enmod rewrite 
    

    After any of those steps, you should restart apache:

    sudo service apache2 restart
    
  1. 允许覆盖

    your_site.conf文件中(例如/etc/apache2/mods-available在我的情况下),添加以下几行:

    <Directory "<path_to_your_html_dir(in my case: /var/www/html)>">
        AllowOverride All
    </Directory>
    
  2. 启用重写模块

    在您的机器上运行此命令:

    sudo a2enmod rewrite 
    

    在任何这些步骤之后,您应该重新启动 apache:

    sudo service apache2 restart
    

回答by th3pirat3

This is in edition to all other right answers:

这是所有其他正确答案的版本:

If you are not able to find the correct Handler, Simply create a .php file with the following contents:

如果您找不到正确的处理程序,只需创建一个包含以下内容的 .php 文件:

<?php echo $_SERVER['REDIRECT_HANDLER']; ?>

and run/open this file in browser.

并在浏览器中运行/打开此文件。

Output from the php code, copy this output

php代码的输出,复制这个输出

Use this output in .htaccess file

在 .htaccess 文件中使用此输出

Create a .htaccess file at the root of your website(usually a folder named public_html or htdocs on linux servers) and add this line:

在您网站的根目录创建一个 .htaccess 文件(通常在 linux 服务器上名为 public_html 或 htdocs 的文件夹)并添加以下行:

AddType [[THE OUTPUT FROM ABOVE FILE]] .html .htm

Example

例子

AddType application/x-httpd-php70 .html .htm

Important Note:

重要的提示:

If you see blank page or Notice: Undefined index: REDIRECT_HANDLER

如果您看到空白页或 Notice: Undefined index: REDIRECT_HANDLER

Try default in .htaccess

尝试默认 .htaccess

AddHandler application/x-httpd-php .html

回答by ilight

For anyone out there still having trouble,

对于任何在那里仍然遇到麻烦的人,

try this (my hosting was from Godaddyand this is the only thing that worked for me among all the answers out there.

试试这个(我的主机来自Godaddy,这是所有答案中唯一对我有用的方法。

AddHandler x-httpd-php5-cgi .html

回答by Mouhssine

I think this is the best way to run php script on html and htm pages:

我认为这是在 html 和 htm 页面上运行 php 脚本的最佳方式:

AddType application/x-httpd-php5 .html .htm

回答by CatsDontWearPants

Normally you should add:

通常你应该添加:

Options +ExecCGI
 AddType application/x-httpd-php .php .html
 AddHandler x-httpd-php5 .php .html

However for GoDaddy shared hosting (php-cgi), you need to add also these lines:

但是对于 GoDaddy 共享主机 (php-cgi),您还需要添加以下几行:

AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html

Source: Parse HTML As PHP Using HTACCESS File On Godaddy.

来源:使用 Godaddy 上的 HTACCESS 文件将 HTML 解析为 PHP

回答by andrewk

here put this in your .htaccess

在这里把它放在你的 .htaccess 中

AddType application/x-httpd-php .php .htm .html

more info on this page

页面上的更多信息

回答by Philipp Michael

Using @Marc-Fran?ois approach Firefox prompted me to download the html file

使用@Marc-Fran?ois 方法 Firefox 提示我下载 html 文件

Finally the following is working for me (using both):

最后,以下对我有用(同时使用两者):

AddType application/x-httpd-php .htm .html AddHandler x-httpd-php .htm .html

AddType application/x-httpd-php .htm .html AddHandler x-httpd-php .htm .html