使用.htaccess将所有.html页面作为.php文件运行?
我需要将所有.html文件作为.php文件运行,而且我们没有时间在明天的演示之前更改所有链接。 有什么办法可以用我的Apache服务器“破解”这个吗?
在你的网站的根目录下创build一个.htaccess文件并添加下面的代码:
[Apache2 @ Ubuntu / Debian:使用此指令]
 AddType application/x-httpd-php .html .htm 
如果你正在将PHP作为CGI运行(可能不是这样),你应该写:
 AddHandler application/x-httpd-php .html .htm 
在我的Godaddy服务器下面的代码工作
 Options +ExecCGI AddType application/x-httpd-php .php .html AddHandler x-httpd-php5 .php .html 
对于任何人仍然有麻烦,
尝试这个(我的托pipe是从Godaddy ,这是唯一的工作,我在所有的答案在那里。
 AddHandler x-httpd-php5-cgi .html 
您需要将以下行添加到您的Apacheconfiguration文件中:
 AddType application/x-httpd-php .htm .html 
你还需要另外两件事情:
- 
允许重写 在 your_site.conf文件中(例如/etc/apache2/mods-availableyour_site.conf在我的情况下/etc/apache2/mods-available),添加以下行:<Directory "<path_to_your_html_dir(in my case: /var/www/html)>"> AllowOverride All </Directory>
- 
启用重写Mod 在你的机器上运行这个命令: sudo a2enmod rewrite在执行任何这些步骤之后,您应该重新启动apache: sudo service apache2 restart
我认为这是在html和htm页面上运行php脚本的最佳方法:
 AddType application/x-httpd-php5 .html .htm 
通常你应该添加:
 Options +ExecCGI AddType application/x-httpd-php .php .html AddHandler x-httpd-php5 .php .html 
但是对于GoDaddy共享主机(php-cgi),您还需要添加这些行:
 AddHandler fcgid-script .html FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html 
来源: parsingHTML作为PHP在Godaddy上使用HTACCESS文件 。
这里把这个放在你的.htaccess中
 AddType application/x-httpd-php .php .htm .html 
更多信息在这个页面上
你也可以使用mod_rewrite的H或T标志强制所有.html文件被php处理程序parsing:
使用H(Handler)标志:
  RewriteEngine on RewriteRule \.(html|htm)$ - [H=application/x-httpd-php5] 
使用T(types)标志:
  RewriteEngine on RewriteRule \.(html|htm)$ - [T=application/x-httpd-php5] 
或者,您可以添加更多扩展到由pipe道分隔的规则模式 你想被parsing的PHP处理程序
例如:
 RewriteRule \.(html|htm|txt|foo)$ - [T=application/x-httpd-php5] 
上面的例子会将以.html , .htm , .txt , .foo结尾的文件types改为php。
注意:在一些服务器上,你将不得不把php5改成php来让这个例子在处理程序string中工作:
更改
 [T=application/x-httpd-php5] 
至
 [T=application/x-httpd-php] 
使用@ Marc-François方法Firefox提示我下载html文件
最后,以下是为我工作(使用两个):
 AddType application/x-httpd-php .htm .html AddHandler x-httpd-php .htm .html 
对于Godaddy服务器,它为我工作
 Options +ExecCGI AddHandler fcgid-script .html FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html 
 AddHandler application/x-httpd-php .php .html .htm // or AddType application/x-httpd-php .php .htm .html