Member-only story

Execute PHP scripts into HTML file by Modifying ‘.htaccess’ file

Asreshashank
4 min readJun 9, 2020

--

PHP and HTML are popular web development technologies, further CSS adds essence to HTML website by making it look decent and user friendly. All 3 components together can help us build the full fledged dynamic website. PHP, short for “Hypertext Preprocessor” supports all the HTML and CSS tags, on top of that, PHP allows us to hard-code Website with PHP scripts. Hence, with basic knowledge in hand, you can start coding and scripting and eventually create your own website.

Now, the Interesting thing is you can use HTML tags in PHP code file, However, you can’t execute PHP script in HTML(.html/.htm) file. In other words, HTML doesn’t understands anything written within<?php …….?>. Even if you wrote php scripts and add it into your HTML file, it simply ignores the script and doesn’t provide any output. Now consider a scenario where, an individual has already built the website using HTML/CSS without realizing that he/she might need a database connection to retrieve data from Database, or lets say the initial requirement changed and now user wants to write some PHP scripts to create features for the website. what can be done in this case?

User might think of shifting an entire website from HTML to PHP and this could be considered as one of the possible solutions, however, it is time consuming at the same time. The another way is to create separate PHP file and call it under <form> tag of HTML as show below.

<form action=”PHPfileName.php” method=”post”> …. </form>

However, what if we have to use php scripts on each of the HTML pages of the website and for some reason we failed to use <form> tag in one or more pages, in that case, we might need to re-write or modify entire code to make a use of it. In such scenarios, the best possible solution is to modify ‘.htaccess’ file.

Hypertext access file or .htaccess file is hidden file using which one can configure additional features of website hosted on Apache server. This file is located at public_html folder in cpanel file manager, you will need to enable ‘show hidden files’ option from settings to locate this file. you can open this file using text editor of your preference, for ex. Notepad, Notepad++, etc. This file generally have few dynamically generated lines of code written in it based on your hosting manager. Now, there are few ways to modify this file, In- order to execute PHP script inside HTML file. One way is to add below line of…

--

--

No responses yet

Write a response