In today's driving back to talk about “.htaccess“, this time we will use the “mod_rewrite” to change the extension to files so that by making a request to “file.html” the request is turned to “file.php” without altering the URL.
Today's tutorial is very short and very easy. The following lines of code do all the work:
Options FollowSymlinks RewriteEngine on RewriteRule ^(.*)\.html$ $1.php [nc]
Save the file called “.htaccess“, within the directory where extensions have to be rewritten try to call a PHP file giving it as HTML extension.
If everything works there should be no differences between the file PHP and the file HTML. In reality it is a single PHP file “masked” da HTML. Now all files with PHP extension in the directory where the file resides “.htaccess” They will be displayed even if invoked with the HTML extension.
To get an idea of the functioning this is the PHP page original, inserting the two terms in the box will be called a second file called “result.html“. On the server, instead, the file is loaded “result.php” showing all those words on the previous page. It, this procedure also works with scripts. The URL the user will continue to see “result.html“.
This technique is very useful for example in the case where the internal links of the site we are working on were pointing to an HTML page, which was originally static, but now we want to make it dynamic. Change the links of all the pages would be a huge waste of time, in this way it will be enough to load a single file “.htaccess” with these instructions and the PHP page will be invoked with all of its features even though the extension will continue to be displayed HTML.