Using PHP code in .html or .htm files

I added a line

AddType application/x-httpd-php .html 

into my Apache configuration file, but I still can't get the php script inside the .html file for parsing. What am I doing wrong?

Is this the wrong configuration file to add this information to?

+4
source share
4 answers

You can use .htaccess:

 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$.html $1.php [L] RewriteRule ^(.*)$.htm $1.php [L] 
+2
source

The handler used may be slightly different.

I would recommend skipping the PHP file as a last resort, as this can be solved with less effort, which is one brute force method.

Listed below are some of the more common options, but there may be more.

  AddType application/x-httpd-php AddType application/x-httpd-php5 AddHandler application/x-httpd-php AddHandler application/x-httpd-php5 

As already mentioned, a combination of AddType and AddHandler may be required.

+2
source

I know that this is not exactly what you quest, but you can fake this HTML file that the user calls using Apache RewriteEngine like this.

 RewriteEngine ON RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^((.+)\.(html|htm))$ ($2).php [L] 

I also think that this is a good security point not to run .html as a PHP script, if you forget you have it, or you can upload an HTML server to your server. (No config apache to run the file in a file with .html as PHP files.)

+1
source

Some web hosts require both directives. Try the following:

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

If you do not require this for your entre site, we also strongly recommend using .htaccess for this in every file / directory.

+1
source

Source: https://habr.com/ru/post/1444211/


All Articles