TheJoe.it Into the (open) source

6Jan/117

Use “.htaccess” to rewrite the extensions of the pages

php_html

In today's driving back to talk about ".htaccess", this time we will use the "mod_rewrite" to change the file extension to 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 the extensions have to be rewritten and 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" will also be displayed if invoked with the HTML extension.

To get an idea of ​​how 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. In 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 called in all of its features even though the extension will continue to be displayed HTML.

About

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.

Comments (7) Trackbacks (0)
  1. The stop as I was wrong. The forum of George Tave is clearly more suitable to your question.
    But I must point out again that if you decide to overshadow the old pages and just keep the new ones might have an impact on search engines, on the contrary if I left the old pages visited you should have no problems. All this “I think”, I might be wrong.

  2. the sitemap and file robort.txt invaritati you leave or you have to make the change extension?

  3. Certainly remains unchanged for all search engines, pages “html” are not obscured, as I have already said if you do not set a specific rule further nell'htaccess the same content will be accessible by one or the other extension (html o php).
    If you do not seem logical're free to argue, but at least use some argument as I did, not “Remains to Be Seen”.

  4. bhe, certainly change there rinominado pages. php. He's using that code to see if you will make sure that everything remains unchanged for search engines. Thank you for having responded cm ²

  5. I'm not really an expert in SEO (;)) but as I see it you should not be penalized, at the end of the search engine does not change anything, the old pagie (unless you do not write other rules) remain accessible to him. But I could be wrong.

  6. I'll explain; rinonimo pages. html to php and insert the code in the. htaccess not to allow the pages continue to be seen and indexed. html

  7. Great guide but I have a doubt. If I rename the pages already indexed. Html to php and insert the code in the. Htaccess what happens in the search engines? I lose positions? pagerank?


Leave a comment

No trackbacks yet.