Jump to content
xisto Community
Sign in to follow this  
BuffaloHelp

Htaccess - Making Html To Parse Php making html file to run php codes

Recommended Posts

I came across an interesting trick while I was trying to make multiple domains to be parked but each would have different index page. (See my previous post).

You don't have to rename file_as.html to file_as.php. You can eliminate the hassle of losing search engine link. There's another trick to redirect incoming ****.html to be redirected to ****.php, but for this demonstration purpose I'm just going to talk about including regular PHP expressions to regular HTML page.

I was faced with editing 100+ HTML files to have URL changed within each page. Although I could have easily done it with HTML editors, copy, paste, save and then upload, I wanted to know if I could do it in a way that would be better for the future. I mean what if I have to change this URL to another format, or include additional information to 100+ HTML files? Do I go through of editing them all over again? If they were a PHP file I would simply include 1 file which can be edited. Then the 100+ pages will reflect the change immediately by uploading a single changed file.

Using htaccess native command,

RemoveHandler .html .htmAddType application/x-httpd-php .php .htm .html

Save this as .htaccess and upload it to the directory where your files will be affected.

Once uploaded, simple have your HTML file to include a code like
<body>
...
...
<?php
include ('filename.php');

//or

for ($i=0; $i<sizeof($array); $i++) {
  echo $array[$i];
}
?>
...
...
</body>
</html>



As you can see, although your file still possess file_as.html, you can make it to run PHP code. And all PHP codes will run normally--no exceptions or exclusions.

Share this post


Link to post
Share on other sites

I knew that for a long time, but be careful of using this, because when apache sends a simple html file, it sends the headers too by default if they are configured, but when you make it a php parsed file, the headers might change, so you need to control them with php, besides when you will want to run just simple html files, it will be slower rather than php.I personally prefer to use mod rewrite and use url links like /category/section/mypage.html there you just control .html with PHP or something like that.. :P

Share this post


Link to post
Share on other sites

Hi!@BuffaloHelpThe settings that you've provided for the HtAccess file hook up the PHP processor to files with the HTML interface. An alternative to get scripts from files that do not include the .php extension would be to read them through a PHP script and to evaluate the string. It achieves the same effect but would involve a greater overhead from having to do it through PHP instead of orchestrating the whole process through a C-coded application (both Apache and the PHP processor). The advantage of using HtAccess, however, is that it gives you the benefit of keeping the original URLs. You can also use the Mod_Rewrite module to get the .html in the URLs to map to the .php extension of the files without having to rename the files to .html. It's a better solution if you already have existing scripts uploaded to the server.The file extensions do not have an effect on the search engine results because you have lots of content management systems that add in their file extensions to the URL and URL re-writing modules that take away the file extension entirely! On most blogs today, we see the article title as the name of the 'file' in the URL and there's definitely no association with the .html extension in that case. In fact, in traditional UNIX systems, file extensions are just vestigial appendages that were added on for the administrators to be able to figure out what they do - the file processing utilities are able to handle files with any kind of extensions. When creating the HtAccess file using Windows Explorer (of course, when trying to run Apache on a Microsoft Windows-based system), you can't really create a .htaccess file because it has no characters before the period - you cannot create a file that has only an extension and no 'primary' name in Windows. However, you can modify an existing .htaccess file normally so the only way to create a .htaccess file would be to programmatically create one, or create one on a UNIX based system and copy it across to the Windows based system either through FTP, SCP, or sending across an archive.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.