使用 .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
Using .htaccess to make all .html pages to run as .php files?
提问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:
您还需要另外两件事:
Allow Overridding
In
your_site.conf
file (e.g. under/etc/apache2/mods-available
in my case), add the following lines:<Directory "<path_to_your_html_dir(in my case: /var/www/html)>"> AllowOverride All </Directory>
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
允许覆盖
在
your_site.conf
文件中(例如/etc/apache2/mods-available
在我的情况下),添加以下几行:<Directory "<path_to_your_html_dir(in my case: /var/www/html)>"> AllowOverride All </Directory>
启用重写模块
在您的机器上运行此命令:
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.
并在浏览器中运行/打开此文件。
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
回答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
回答by andrewk
回答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