Friday, December 28, 2012

Execute PHP from .html or extensionless file :: Step-by-step instructions for Apache and IIS servers

If your website is receiving very huge traffic and your website deals with high use of web-programming or server-side scripting then it is a very good idea to hide the Scripting language you use. For example if you use PHP, which is very vulnerable to security attacks, you can use it in extensionless file. If the extension (.php, .php3, .php5) is not visible to web browsers then it is really helpful.

Today I am sharing on how to add a web.config or .htaccess file to the root of your web server to make server execute PHP in .html, .htm or even in extensionless files.


For Linux and Windows Servers running Apache


PHP is a very famous scripting language to be used under Apache servers.


To enable your server to run PHP from .html or other extension files, follow these steps :


1. Login to your Hosting Provider and open FTP File Manager.

2. At the root Directory create a file .htaccess . You see the file has no name but only extension. If your web provider does not allow this file to be created, then create a file on your local machine and upload it into the root directory.

3. Open the .htaccess file in a text editor and add the following line

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

Explanation : This code sets .html and .htm files to be handled with PHP via FastCGI.

You can add as many extentions as you want, Just give a space and type the extension. Don't add those extensions like .py or .rb as it may already correspond to other server side scriptings (.py for Python and .rb for Rubi). Ask your Hosting Provider for a list of supported languages before doing so.

4. Save the file.

To test whether it works or not follow these steps :

1. Open a text editor and type the following code :

<?php echo "Hello World This is PHP executed in HTML file"; ?>

2. Save the file as test.html

3. Open the file in browser. You should get the response

Hello World This is PHP executed in HTML file

4. Done


To enable your server to run PHP from an extensionless file, follow these steps :


1. Login to your Hosting Provider and open FTP File Manager.

2. At the root Directory create a file .htaccess . You see the file has no name but only extension. If your web provider does not allow this file to be created, then create a file on your local machine and upload it into the root directory.

3. Open the .htaccess file in a text editor and add the following line :

<Files *>
ForceType application/x-httpd-php
SetHandler application/x-httpd-php
</files>


Explanation : * determines the server to run any file under PHP.

4. Save the file.

To test if it works or not, follow these steps :

1. Open a text editor and type the following code :

<?php echo "Hello World This is PHP executed in an extensionless file"; ?>

2. Save the file as "test". Remember to save it without extension.

3. Open the file in browser. You should get the response

Hello World This is PHP executed in extensionless file.
 
4. Done

For Windows server running IIS 7 or Higher


IIS (Internet Information Services) basically does not support PHP, it must be manually configured. The Below mentioned steps will only work if you have already configured PHP to run under IIS.

To enable your server to run PHP from .html or other extension files, follow these steps :


1. Login to Hosting Provider and open FTP File Manager. 

2. At the root Directory create a file name "web.config" if it already does not exists.

3. Open web.config in any text editor and put the following code in it :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="html via php cgi" path="*.html" verb="*" modules="FastCgiModule" scriptProcessor="C:\Php5\php-cgi.exe" resourceType="Unspecified" />
</handlers>
  </system.webServer>
</configuration>

Add as many handlers as you want. Change the value of  "path"  and "name" attribute each time. Don't use Extensions like .asp or .aspx as it may correspond to any existing Scripting (.asp for Active Server Pages and .aspx for ASP.NET).

The value of "scriptProcessor" attribute may vary depending on the folder where PHP is installed. Ask your Web Hosting Provider if you are not sure. For GoDaddy Hosting, it works fine.

4. Save the file.

To test whether it works or not, follow these steps :

1. Open a text editor and type the following code :

<?php echo "Hello World This is PHP executed in HTML file"; ?>

2. Save the file as test.html

3. Open the file in browser. You should get the response

Hello World This is PHP executed in HTML file

4. Done

To enable your server to run extensionless file under PHP, follow these steps :

 

1. Login to Hosting Provider and open FTP File Manager. 

2. At the root Directory create a file name "web.config" if it already does not exists.

3. Open web.config in any text editor and put the following code in it :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="html via php cgi" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Php5\php-cgi.exe" resourceType="Unspecified" />
</handlers>
  </system.webServer>
</configuration>

The value of "scriptProcessor" attribute may vary depending on the folder where PHP is installed. Ask your Web Hosting Provider if you are not sure. For GoDaddy Hosting, it works fine.

4. Save the file.

To test whether it works or not, follow these steps :

1. Open a text editor and type the following code :

<?php echo "Hello World This is PHP executed in extensionless file"; ?>

2. Save the file as "test"

3. Open the file in browser. You should get the response

Hello World This is PHP executed in extensionless file

4. Done

Warnings


Remember that whenever you edit such files, you have a high risk of web server not accepting it or resulting in errors. Work on these steps only if you are experienced. Be careful and if you result in errors then delete .htaccess or web.config file created or contact your Service Provider immediately.

Specifically be careful when editing web.config file. You may result in losing your default file access or access to other file request handles


For more help, Mention your issues in comments.

5 comments:

  1. Hi,

    Recently I came across some great articles on your site.
    The other day, I was discussing ()with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
    Would it be ok to submit the article? It is free of charge, of course!

    Let me know what you think
    Contact me at anelieivanova@gmail.com

    Regards
    Anele Ivanova

    ReplyDelete
  2. < add name="html via php cgi" path="*.html" verb="*" modules="FastCgiModule" scriptProcessor="C:\Php5\php-cgi.exe" resourceType="Unspecified" />
    Here scriptProcessor path changes form server to server. To find the path check phpinfo() of the server ,find 'Loaded Configuration File'. The folder in which 'php.ini' is stored will have 'php-cgi.exe'. Suppose the php.ini path is 'C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\php.ini', then the scriptProcessor path will be 'C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\php-cgi.exe'.

    Hope this helps for someone.
    Thanks

    ReplyDelete
  3. This as saved me hundreds of dollars. Thank you.

    ReplyDelete
  4. for godaddy which code is apply for php script run on .html files.
    please give your best reply as soon as possible.

    ReplyDelete

Please don't use slangs. Be polite