Once in a way, I get the occasional query from visitors to thesitewizard.com asking how they can hide their CGI or PHP scripts so that while the visitor thinks he/she is receiving a page like http://example.com/pagename.html, he/she is actually getting the output of your script at http://example.com/scriptname.cgi. The URL that is displayed in the visitor's browser remains "pagename.html", but the web server actually executes "scriptname.cgi". There are a few ways in which this can be accomplished without the visitor knowing any different. This article will show you a couple of methods, depending on what you're trying to accomplish.
For the methods listed below to work, the following are required:
This section deals with one way to make certain dynamically generated pages appear as static HTML pages. To mask, say, http://example.com/scripts/scriptname.php as http://example.com/scripts/pagename.html, take the following steps:
Use an ASCII text editor and type in the following lines:
Remember: you need an ASCII text editor for this. Do not use a word processor like Microsoft Word, Star Office or Wordpad. If you use Windows, you can find an ASCII text editor called Notepad in your Programs | Accessories menu. Or you can get one of the many free text editors available on the Internet from thefreecountry.com's Free ASCII Text Editors page at https://www.thefreecountry.com/programming/editors.shtml
Save the file as ".htaccess" (without the quotes) and upload it into the directory of the script. In the case of the above example, save it into the scripts directory.
If you are using Notepad, you have to save it as ".htaccess" (with the quotes) in order to force Notepad to save your file as ".htaccess". Otherwise it'll automatically add a ".txt" suffix.
Create your script and upload it into that same directory, giving it the name scriptname.php (or whatever name you put into your RewriteRule in your .htaccess file).
You should now test it by calling the script in your browser using http://example.com/scripts/pagename.html (the "fake" name) to see if it works.
Here's how to customise the above .htaccess for your situation.
If you place your scripts in another directory, change the RewriteBase line accordingly. For example, if you place it in a directory called "fakefiles", change it to
If you are trying to masquerade a CGI script named, say, scriptname.cgi as pagename.html, change the RewriteRule to:
Basically the above .htaccess lines tell the mod_rewrite module to rewrite any requests to, say, pagename.html internally to scriptname.php. At the same time, it changes the MIME type of the document to that of the PHP MIME type so that the server will do the right thing with the document. The visitor's browser will continue to display the pagename.html URL, since all this rewriting occurs behind the scenes without the client (browser or search engine) knowing what has transpired.
If your intention is to have your whole site appear as static HTML pages while in reality being generated by (say) PHP, instead of only isolated pages as is assumed above, a simpler method than that outlined above is to simply add the following line to your .htaccess file:
You would then name all your PHP scripts with a ".html" extension instead of the usual ".php".
In such a case, you would not need mod_rewrite to be loaded as an Apache module.
When Apache serves a static web page, it will also issue a "Last-modified" header to the requesting client (such as a visitor's web browser). This "Last-modified" header tells the browser (or proxies or other clients) the date that the document was last changed. It allows browsers and proxies to know when it must reload the document to get the latest version. The header is not issued when a dynamically generated document is served - that is, it is not served when documents with server side includes (SSI) (such as *.shtml documents) and documents generated by scripts (such as PHP or Perl) are served.
When you use either of the above methods to serve your dynamic pages, the web server will also not generate this "Last-modified" header even though it may appear to your visitors as though it is receiving a ".html" file.
This has the advantage that your page will probably not be cached by proxy servers or certain browsers. This ensures that visitors get the latest version of your page.
The disadvantage of this, however, is that if you are really trying to pretend your page is a HTML document (for whatever reason), this lack of a "Last-modified" header will give the game away.
If your Apache web server has something known as the XBitHack compiled in, it is possible to force the server to issue the "Last-modified" header. The header will give the time your script was last modified as the date, regardless of whether your script will generate new information on each invocation.
To enable it, add the following line to your .htaccess file:
Then make sure that your script has the group-execute bit set. If you usually chmod your scripts to either 0755, 0711, 0750, or 0710, you would have already fulfilled this requirement.
Think carefully before you enable this setting. There are many people in the world surfing the web through proxies. When you enable the XBitHack setting, these people might end up viewing an old copy of your page saved in the proxy's cache. This is especially so if your script can generate different output even when you have not modified the script file itself.
In general, if you do not have a compelling reason why your dynamically generated page must appear as a static document, it is best to avoid using the above methods. All this chicanery adds to your server load, possibly affecting the speed of the delivery of your documents (depending on how many visitors are hitting your site each moment, of course).
Furthermore, if your purpose is simply to avoid dead links on your site (for example, when you've changed a previously static page to a dynamic one), the above method is not only an overkill, but your visitors will probably also never update their bookmarks since, to them, the URL still works fine. In fact, new visitors bookmarking your site will also end up adding that obsolete URL to their bookmarks, since that is the URL that will appear in their browser's location bar. In such an instance, you might be better off using the "Redirect" directive in your .htaccess file. (See the article Changing Web Hosts - Tips on Moving Your Website at https://www.thesitewizard.com/archive/movinghosts.shtml for more information on this directive.)
Nonetheless, it is a useful tool, and when used appropriately, can go a long way to make your site more usable.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as: