Htaccess Rule to Parse PHP in HTML Files Not Working

htaccess rule to parse php in html files not working

Is your server using suPHP rather than mod_php or plain php-cgi?

Try using

AddHandler x-httpd-php .html .htm

or

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

instead.

Postscript

Shared Hosting services use UID based separation on individual accounts from each others. Most use suPHP, but some use suEXEC. They will use one of these. Both act as a su wrapper around php-cgi so you can't tell from the phpinfo()as its PHP scripting engine as this will report Server API CGI/FastCGI in both cases. For CGI initiated scripts, phpinfo doesn't report on the Apache config. You need either to look at your hosting provider's FAQ or possibly try:

 <?php
header( "Content-Type: text/plain");
echo system('grep -iR LoadModule /etc/httpd /etc/apache2');

The hosting provider's support forums / FAQ might give specific configuration advice here. Have you tried them?

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

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

[Apache2 @ Ubuntu/Debian: use this directive]

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:

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

Using .htaccess to run PHP code within index.html

The reason why the web server is not passing the index.html file thru the PHP interpreter is that the mime-type has not been associated with it.

You need to use the AddHandler directive to set it.

In order to associate the type you've added, it really depends on what your setup is. Various scenarios might be: php-fpm, mod_php, cgi.

An example using CGI is:

Action application/x-httpd-php5 "/path/to/php"

Bear in mind that this setting is usually not available on shared hosting. If you are using one, consider contacting the support helpdesk.

Consider having a look at this duplicate question: Parsing HTML files as PHP

I would suggest against adding a handler. You should rather rename the index.html file to index.php. If this is not picked by Apache, you can correct it using DirectoryIndex index.php.

I hope this helps.

htaccess rewrite .htm to html not working

You need to activate the mod_rewrite first.

Try:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.htm$ $1.html [R=permanent]

The R=permanent flag tells Apache to issue a permanent redirect 301 response, which is search-engine friendly.

Also, there are these great resources available online:

  • Tips for configuring Apache's mod_rewrite
  • modrewrite.com
  • Learn Apache mod_rewrite: 13 Real-world Examples

Also worth checking:

  • 10+ Mod_Rewrite Rules You Should Know


Related Topics



Leave a reply



Submit