December 13, 2004

Converting to PHP

This tutorial is written by LMT guest author Sarah Hughes of This Chick (aka Maddy in the MT Forums).

Many customizations for Movable Type call for using PHP scripts that require that your pages have a .php extension rather than a .html or .htm extension.

PHP is a server-side scripting language which involves a) the server looking at your pages for a PHP script, b) running the script, and c) outputting the results to the page. This is called "parsing". In order to successfully use PHP in your pages, you need to ensure that your account on your webserver is set up to parse your pages for PHP scripts. This feature is pretty standard these days, but check with your webhost before attempting a conversion to PHP.

"Converting to PHP" simply means changing the pages of your site to have a .php extenstion rather than an .html extension. Your pages will still display HTML as usual, but with the added benefits of of PHP scripting. For example, you will be able to use dynamic content, such as random photos or quotes, which alter with each page load (unlike MT plugins, which alter on each site rebuild.) Being able to include certain information into each page is easier and quicker with PHP includes. If you're using a mySQL database to power your site, the options are even more greatly expanded.

These instructions will detail how to change the extension on all of the files created by Movable Type (except the .cgi ones).

  1. Login to your Movable Type installation. Go to Weblog Config, and select Preferences. On this page you will find a place to set your "File Extension for Archive Files". This box by default contains html. Change this to php. Save this change.
  2. While still in Weblog Config, go to Archive Files (or Archiving, in a pre-3.0 version). If any of the Archive File Template boxes are filled in, make sure to change any references to a .html extenstion to a .php extension. Save, but don't rebuild yet. If you hadn't made any changes there, don't worry about it, and move on to the next step.
  3. Go to the Templates screen, and select your Main Index Template. Change the filename in the Output File box to have a .php. Save, but don't rebuild.
  4. Go back to the Templates screen, and repeat the previous step for your Master Archive Index, and any other pages you want to use PHP in. Leave files like the RSS, Atom, Stylesheet templates alone - they have their own required extensions.
  5. Now you're ready to rebuild your site. Note that by doing this, MT will create duplicate copies of the archive and index pages on your server, one with .php extensions and one with .html extensions.
  6. The last step is to delete your old index.html file. Once you've done that, your new index.php file should pop up automatically when you visit your Site URL. (I'll discuss what to do if it doesn't a little later on in this tutorial.)

Dealing with Duplicates

As mentioned in step 5, rebuilding your site after changing the file extensions will result in duplicate copies of your pages on the server, one with the .html extension, and the newer one with the .php extension. Because of this you are now using twice as much server storage space as you really need. If you have space to spare, you might be happy enough to leave it as it is. If not, you might want to delete the duplicate .html files.*

Before you make the decision on what is right for you, there are a couple of things to bear in mind. First, the files with the .html extensions will no longer be updated. Second, people may have linked to your entries with those .html extensions. If you delete them, and someone follows a link, they will get a 404 Page Not Found error.

So, what to do? You might consider creating a custom error page, telling your readers that you've made a change to your file extensions, suggesting how they can find the correct page. Or you could set up a redirect to send them there automatically.

For help with creating a custom 404 page, see this tutorial for some ideas. Also, it's a good idea to add a search box for you MT installation to the 404 page, as well.

* One thing you should note is that MT does not rebuild the files for pop-up image files after they are first created, so do not delete these. From this point on, any new files of this kind will have the .php extension, and any existing links your have to these image files will continue to be correct.

Loading index.php by default

What if you've deleted your index.html, but when you view your site, you're just getting a directory listing, and not your new index.php file?

  1. Look in your root web directory for a file called .htaccess. Sometimes this file will be hidden, but you should be able to set your FTP progam to show hidden files. The .htacess files contains a set of instructions that tell the webserver how to handle some things related to your site. (See What is .htaccess?)
  2. Download that file to your desktop, and open it up in a text editor like NotePad, or my favourite EditPad Lite (or BBEdit for Mac). If the file contains other information, be careful not to edit any of it!
  3. Look to see if the file contains a line beginning with DirectoryIndex. If it does, that's the line you'll need to edit now.
  4. If there is no such file on your server, simply create a text file and continue with the rest of the instructions.
  5. If you didn't have an .htaccess file, or your existing one didn't contain that line, add a new line to the file cotaining this code:
    DirectoryIndex index.php index.html index.htm

    This line gives an instruction to the webserver to serve the index files in the order of preference listed. So, if there is an index.php it will be loaded. If there is not, then the next option in the list index.html will be shown, and so on.

  6. Save the file you've been working on. Your text editor might decide to add a .txt extension to the file. That's fine. Just rename the file after saving to be .htaccess.
  7. Now, upload your file into the root directory of your server, overwriting the existing file if necessary.
  8. Now when you got to your Site URL, your index.php should appear.

It's all too complicated?

There is an easier, alternative way. What follows is a method for having our pages parsed for PHP scripting which involves no changes to your MT set up, and avoids the need for duplicate pages or redirects. I could have posted this first, but learning how to alter your MT config settigs is a good skill to learn.

  1. Follow the first 2 steps under Loading index.php by default, and get ready to edit the .htaccess file.
  2. Add the following line to that file:
    AddType application/x-httpd-php .html .htm .php

    What this line does is to tell server to look through all of the .html, .htm and .php files in your hosting account (not just the MT generated files as with the previous method) for PHP scripting.

As mentioned, an advantage of this method is that your permalinks will not change. An obvious disadvantage, or inefficiency, is that you might only have PHP scripts in a small number of those pages.

As a sidenote, if you have a tool like cPanel for your hosting account, you may also be able go use it to make this change. Login to your cPanel, and select "MIME Types".

  • In the box labelled "Mime Type" enter application/x-httpd-php, and in the box labelled "Extensions" enter .html .htm .php. Hit "Add" and you should be done.

    A brand new weblog?

    If you don't have an existing weblog, then obviously you have nothing to convert. In order to ready a new weblog to allow for PHP scripting, just follow the first four steps of the conversion process when you are intially configuring your weblog.

    Getting a 500 Internal Server Error

    When you view your new .php pages, are you getting an Internal Server Error? If so, it is likely that your server setup requires that PHP pages have a different permission (usually at least 755) than regular HTML pages.

    There is a section in the MT Troubleshooting Manual explaining how to have the files generated by MT have the necessary permissions.

    By default, all files generated by MT will have their permissions set to 666 (unless your install is running under CGIWrap or suEXEC in which case the files are set to 644). If your server requires a different setting for PHP files, then this will result in an error, because the files are not executable. So, we need to change this by altering the HTMLPerms (and possibly HTMLUMask) setting in mt.cfg.

    1. The default setting for HTMLPerms is 666. In order to alter this setting open up mt.cfg and find the following line:
      # HTMLPerms 0777

      Now, remove the # from the beginning of the line. This tells MT that you want to use a setting other than the default.

    2. Now, we probably want permissions to be 755 (though this may vary depending on your server - your host should know the answer to that question), so edit the line so that it looks like this:

      HTMLPerms 0755

      Now save the edited version of mt.cfg and upload to your server, replacing the old copy. Remember to do this in ASCII mode.

    3. Rebuild your entire site.
    4. If on viewing your site, you're still getting an error, fear not. The setting we just changed should only effect pages when they are created, so the change might not be reflected on a rebuild. To get around this, you will want to delete all of the .php files generated by MT, and then rebuild. The new copies should now have the correct permissions.

    Using PHP and MT Includes

    Has this tutorial been helpful? Please consider linking to Learning Movable Type at . Thanks!

    Posted by Sarah Hughes on December 13, 2004 to General Tips and Tricks, PHP
    Comments(15) | Email to a friend | Printer-friendly version


    If you would like to send a trackback
    please use the following URL:


    Great article and well written. I would however like to point out that if you have built your MT template for your main index off of the existing template provided and left this tag at the top of your document:
    " xml version="1.0" encoding="iso-8859-1" "
    Atleast in my case the file index.php wont render correctly if that tag remains. In fact it will freeze up at various stages of rendering.

    Also wanted to mention that my PHP files are not pulling their includes (be it a .php or .inc file) at all, not even executing the server side call on the MT generated pages, hazard a guess ?why this is occuring.


    Hi Paul,

    That tag you mentioned isn't in the default header of my Index template. I wonder what version of MT you are running?

    As to why your PHP files are not pulling in their includes, I don't know. You might try asking over at the forums. Are you sure your web host supports PHP?

    No, that tag is not in any of the MT default templates, though I have seen external editors add it in if you're adjusting your templates that way. You might also find it in templates from other sites that have been created in such an editor.

    If that line is present, and PHP is correctly enabled, the reaction I would expect would be an Internal Server Error. See this forum thread for more information.

    I have installed the most recent MT release. I would concider your "external editor" argument, only that I saved the source code for the default template installed with MT (3.11 I think?). This original index template actualy had that code. Odd to say the least. I only comment as something to concider as I wrestled with it for a good 30 minutes!

    Yes, PHP does work on any pages I code so it is enabled! Or else I'd nail them for false advertising with their "PHP supported" claims! ;)


    thanks for this great resource by the way

    Yes, that is VERY odd, as the code does not appear in any of the templates contained in (minus the expected RSS/Atom templates) for any of the MT versions I have the distrubtions for (and have checked) from 2.661 through 3.121. ;-)

    If PHP is working some pages, and not others, then you'll need to look to see what's different about the pages generated by MT, and those you code by hand. :-)

    If you're posting PHP in entries, you'll want to make sure the Text Formatting is set to None (so that line-breaks aren't added), for example. And if you're still having problems, I recommend you check out some of these PHP Resources. :-)

    wow, great article. I just did all of the steps and everything is working well (so far). I'm still setting everything up. I'm coming over from Greymatter 1.3, I love MT already!

    THANK YOU!!!!! This problem has being killing me for over a month, and I havn't had the time to look into it. You are my hero!!!!!!!

    That was fantastic. One of the easiest changes I have made to my site.

    Thanks for that - very staight forward

    I did everything you said but noticed that my archive folders are still rebuilding in 777. Is there a way to change that?

    And an even /easier/ method (if you're ok with all files being parsed as php) is by using:

    DefaultType application/x-httpd-php

    I followed the CPanel step.Do i still need to delete my index.html file?

    If you have both an index.php and an index.html on your server, what will get shown is the index.html. So, once you have generated an index.php, you need to remove the index.html page from the server.

    Thanks for a terrific resource. My conversion worked great except for one small item.

    I'm using one of the new templates (powell street) that work with MT 3.2, and in the right column there is a link called "Archives". For some reason this still points to "archives.html" instead of ".php". In spite of making the necessary changes to the .htaccess as indicated above, this link still gives 404 errors. I am guessing that since the link is inside the walls of my site it's treated as implicit and the htaccess file doesn't affect it.

    But the best answer would be to change the link to "archive.php". I could manually change after each rebuild, but that would get old fast. Any idea where the "archive.html" link gets drawn in from? I've searched to no avail. And I'm no code warrior either so I wouldn't know where to begin for a work around.

    Thanks in advance. My site (if you wish to look)is:

    A big THANK YOU for this site as a whole, but this particular post saved me from what probably would have been hours of headaches.


    Post a comment

    (Before posting a comment please see the Comments and Trackbacks Policy. Do you need help troubleshooting your weblog? Please post questions and requests for support at the MT Support Forums. Thanks!)

    Remember Me?

    (you may use HTML tags for style)

    Email to a friend

    Email this article to:

    Your email address:

    Message (optional):