Using .htaccess to make all .html pages to run as

2018-12-31 04:11发布

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?

13条回答
情到深处是孤独
2楼-- · 2018-12-31 04:18

In My Godaddy Server the following code worked

Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html
查看更多
人间绝色
3楼-- · 2018-12-31 04:19

I'm using PHP7.1 running in my Raspberry Pi 3.

In the file /etc/apache2/mods-enabled/php7.1.conf I added at the end:

AddType application/x-httpd-php .html .htm .png .jpg .gif
查看更多
初与友歌
4楼-- · 2018-12-31 04:25

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 echo $_SERVER['REDIRECT_HANDLER']; ?>

and run/open this file in browser.

Output from the php code, copy this output

Use this output in .htaccess file

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:

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

Try default in .htaccess

AddHandler application/x-httpd-php .html
查看更多
妖精总统
5楼-- · 2018-12-31 04:30

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

using H (Handler) flag:

 RewriteEngine on

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

using T (Type) flag :

 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

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 , .foo to php.

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

Change it

[T=application/x-httpd-php5]

to

[T=application/x-httpd-php]
查看更多
皆成旧梦
6楼-- · 2018-12-31 04:30

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

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

You also need two other things:

  1. 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>
    
  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
    
查看更多
呛了眼睛熬了心
7楼-- · 2018-12-31 04:31

here put this in your .htaccess

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

more info on this page

查看更多
登录 后发表回答