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).
- 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.
- 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
.htmlextenstion to a
.phpextension. 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.
- 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.
- 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.
- 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.
- The last step is to delete your old
index.htmlfile. Once you've done that, your new
index.phpfile 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?
- 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
.htacessfiles contains a set of instructions that tell the webserver how to handle some things related to your site. (See What is .htaccess?)
- 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!
- 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.
- If there is no such file on your server, simply create a text file and continue with the rest of the instructions.
- If you didn't have an
.htaccessfile, 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.phpit will be loaded. If there is not, then the next option in the list
index.htmlwill be shown, and so on.
- Save the file you've been working on. Your text editor might decide to add a
.txtextension to the file. That's fine. Just rename the file after saving to be
- Now, upload your file into the root directory of your server, overwriting the existing file if necessary.
- Now when you got to your Site URL, your
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.
- Follow the first 2 steps under Loading
index.phpby default, and get ready to edit the
- 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
.phpfiles 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".
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
- The default setting for
HTMLPermsis 666. In order to alter this setting open up
mt.cfgand 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.
- 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:
Now save the edited version of
mt.cfgand upload to your server, replacing the old copy. Remember to do this in ASCII mode.
- Rebuild your entire site.
- 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
.phpfiles generated by MT, and then rebuild. The new copies should now have the correct permissions.
Using PHP and MT Includes