« Movable Type Demographic Tidbits | Main | Planet Movable Type »

Converting to PHP

This tutorial is written by LMT contributor Sarah Hughes of Sassy (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.

    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.

    Links:
    Using PHP and MT Includes

  • Comments (7)

    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.

    Marya Baron:

    Hi! Great information. Everything is working just fine for me, except the Search results page is still outputting as HTML so my sidebars don't show up. Any advice? Thanks!

    Hi Marya

    Unfortunately, the problem is with the system templates, those templates cannot have any PHP within them (the technical reason - those system templates are built "on-the-fly" by perl scripts such as mt-search.cgi and mt-comments.cgi).

    Your best alternative is to use the MTInclude tag with a file attribute like so:

    <MTInclude file="/location/to/sidebar.html">

    A more advanced alternative is discussed here

    Marya Baron:

    Thanks Arvind! I will use that advice. I suppose that means I should re-output my sidebars with an html extension rather than .inc which is what they are now?

    Incidentally, I'm sure you know an easy trick to make the search action stop using the wrong URL for the cgi wrap? I have multiple blogs on my mt installation, all with different URLs, and it seems insistent on redirecting to the 'master' or original blog's URL rather than the blog the search is for. I have tried editing the areas where MTCGIPath is and replacing with an absolute path but it doesn't seem to work. I peeked at mt-search.cgi but perl is a little beyond me so I couldn't puzzle out whether that script is responsible for the redirect.

    I think there might be something funky about my install, maybe? My web hosts did it (pair networks) and I am also having trouble using your lovely blogroll plugin (internal server error when I try the link) and plugin manager as well.

    Thanks, regardless- I am going to give your helpful link a try too, perhaps with the include for the time being.

    Marya Baron:

    Hi again-- I see it works just fine with the .inc extension. Thanks again!

    MW:

    This page needs an update for those using PHPsuExec.

    The MIME type modification method is similar, but different if you have PHPsuExec running.

    Instead of using (for the php apache mod)

    AddType application/x-httpd-php .html .htm .php

    Use (in your .htaccess)

    AddHandler application/x-httpd-php .html .htm .php

    If you use PHP5 then use (in .htaccess)

    AddHandler application/x-httpd-php5 .html .htm .php

    A simple solution.. but oh dear.... so long to find it out!

    Post a comment

    (If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)