logo logo

execute html as php via .htaccess

Whenever a web site is accessed by users, the web server detect the file extension and decide what would it do with the file; directly served or run it as script. If the file extension is .htm or .html, the web server usually serve this file directly. If the extension is .php, the web server will run it as script using PHP interperter and serve the script’s result to user.

Some common problem: You discover an ideal piece of software, and you need to add this functionality in your website, however you have to use PHP in your web page for this to working properly. You can simply rename your own webpages to php rather than html, however, your URL is already indexed in search engine as .html file. Renaming your file into .php will render this .html page to be not found (404 error). And you need to resubmit the correct URL to search engine and waiting to be indexed again. What a pain!

There’s a simple solution for that. Just run the PHP codes inside HTML files! But this feature is not enabled by default. You need to add this line below in your .htaccess:

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

What does that line tell? It tells the web server to treat .html and .htm as PHP script, therefore need to run it against PHP interpreter before serving it to users.

Most of web host allowed you to change/have your own .htaccess file. Be careful when editing .htaccess file, it may already another server side settings for your website. If you didn’t see the file, try choose to “show hidden files” in your file manager (such as cpanel). If it’s still not there, then there’s no .htaccess file yet (not all site had .htaccess by default), so you can create an empty text file named .htaccess.

bottom

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bottom