<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <title>Posts by Sarah Hughes on Learning Movable Type</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/" />
   <link rel="self" type="application/atom+xml" href="http://www.learningmovabletype.com/contributors/sarah/" />
   <id>tag:,2008-02-25:/5</id>
   <updated>2007-06-22T04:36:23Z</updated>
   <subtitle>Tutorials and helpful tips for the Movable Type web publishing system</subtitle>
   <generator uri="http://www.movabletype.org/">Movable Type Publishing Platform 4.01</generator>

<entry>
   <title>Converting to PHP</title>
   <link rel="alternate" type="text/html" href="http://www.learningmovabletype.com/a/000886converting_to_php/" />
   <id>tag:www.elise.com,2004:/mt//10.886</id>
   
   <published>2004-12-14T04:30:29Z</published>
   <updated>2007-06-22T04:36:23Z</updated>
   
   <summary>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...</summary>
   <author>
      <name>Sarah Hughes</name>
      <uri>http://www.sassy.net.nz/</uri>
   </author>
   
      <category term="General Tips and Tricks" scheme="http://www.sixapart.com/ns/types#category" />
   
      <category term="PHP" scheme="http://www.sixapart.com/ns/types#category" />
   
   <category term="convertingtophp" label="Converting To PHP" scheme="http://www.sixapart.com/ns/types#tag" />
   <category term="php" label="PHP" scheme="http://www.sixapart.com/ns/types#tag" />
   
   <content type="html" xml:lang="en" xml:base="http://www.learningmovabletype.com/">
      <![CDATA[<p><em>This tutorial is written by LMT contributor Sarah Hughes of <a href="http://www.sassy.net.nz">Sassy</a> (aka Maddy in the MT Forums).</em></p>

<p>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.</p>

<p>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.</p>

<p><strong>"Converting to PHP"</strong> 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 <a href="http://www.alistapart.com/articles/randomizer/">random photos</a> 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.</p>

<p>These instructions will detail how to change the extension on all of the files created by Movable Type (except the .cgi ones).</p>]]>
      <![CDATA[<ol><li>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 <code>html</code>. Change this to <code>php</code>. Save this change.<li>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 <code>.html</code> extenstion to a <code>.php</code> 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.<li> Go to the Templates screen, and select your Main Index Template. Change the filename in the Output File box to have a <code>.php</code>. Save, but don't rebuild.</li><li>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.</li><li>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.</li><li>The last step is to delete your old <code>index.html</code> file. Once you've done that, your new <code>index.php</code> 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.)</li></ol>

<p><br />
<h3>Dealing with Duplicates</h3></p>

<p>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.*</p>

<p>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.</p>

<p>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 <a href="http://www.learningmovabletype.com/archives/000732meta_refresh_redirect_tag.php">redirect</a> to send them there automatically.</p>

<p>For help with creating a custom 404 page, see <a href="http://www.learningmovabletype.com/archives/000236custom_error_messages.php">this tutorial</a> for some ideas.  Also, it's a good idea to add a search box for you MT installation to the 404 page, as well.</p>

<p><em>* 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.</em></p>

<h3>Loading index.php by default</h3>

<p>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? </p>

<ol><li>Look in your root web directory for a file called <code>&#46;htaccess</code>. Sometimes this file will be hidden, but you should be able to set your FTP progam to show hidden files. The <code>.htacess</code> files contains a set of instructions that tell the webserver how to handle some things related to your site. (See <a href="http://www.learningmovabletype.com/archives/000743what_is_htaccess.php">What is .htaccess?</a>)</li>
<li>Download that file to your desktop, and open it up in a text editor like NotePad, or my favourite <a href="http://www.editpadpro.com/editpadlite.html" title="Lots of good things, like numbered lines.">EditPad Lite</a> (or BBEdit for Mac). If the file contains other information, be careful not to edit any of it!</li>
<li>Look to see if the file contains a line beginning with <code>DirectoryIndex</code>. If it does, that's the line you'll need to edit now. </li>
<li>If there is no such file on your server, simply create a text file and continue with the rest of the instructions.</li>
<li>If you didn't have an <code>&#46;htaccess</code> file, or your existing one didn't contain that line, add a new line to the file cotaining this code:

<blockquote>

<p>DirectoryIndex index.php index.html index.htm</p>

</blockquote>

<p>This line gives an instruction to the webserver to serve the index files in the order of preference listed. So, if there is an <code>index.php</code> it will be loaded. If there is not, then the next option in the list <code>index.html</code> will be shown, and so on.</li><br />
<li>Save the file you've been working on. Your text editor might decide to add a <code>.txt</code> extension to the file. That's fine. Just rename the file after saving to be <code>&#46;htaccess</code>.</li><br />
<li>Now, upload your file into the root directory of your server, overwriting the existing file if necessary.</li><br />
<li>Now when you got to your Site URL, your <code>index.php</code> should appear.</li></ol></p>

<h3>It's all too complicated?</h3>

<p>There is an easier, <em>alternative</em> 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.</p>

<ol><li>Follow the first 2 steps under <strong>Loading <code>index.php</code> by default</strong>, and get ready to edit the <code>&#46;htaccess</code> file.</li>
<li>Add the following line to that file:

<blockquote>

<p>AddType application/x-httpd-php .html .htm .php</p>

</blockquote>

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

<p>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.</p>

<p>As a sidenote, if you have a tool like <a href="http://www.cpanel.net" title="Webhosting Management">cPanel</a> for your hosting account, you may also be able go use it to make this change. Login to your cPanel, and select "MIME Types".</li><br />
<li>In the box labelled "Mime Type" enter <code>application/x-httpd-php</code>, and in the box labelled "Extensions" enter <code>.html .htm .php</code>. Hit "Add" and you should be done. </p>

<h3>A brand new weblog?</h3>

<p>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. </p>

<h3>Getting a 500 Internal Server Error</h3>

<p>When you view your new <code>.php</code> 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. </p>

<p>By default, all files <em>generated</em> by MT will have their permissions set to 666 (unless your install is running under <a href="http://www.learningmovabletype.com/archives/000770cgiwrap_and_suexec.php">CGIWrap or suEXEC</a> 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 <a href="http://www.movabletype.org/docs/mtmanual_configuration.html#item_HTMLPerms">HTMLPerms</a> (and possibly <a href="http://www.movabletype.org/docs/mtmanual_configuration.html#item_HTMLUMask">HTMLUMask</a>) setting in <code>mt.cfg</code>.</p>

<ol><li>The default setting for <code>HTMLPerms</code> is 666. In order to alter this setting open up <code>mt.cfg</code> and find the following line:

<blockquote>
# HTMLPerms 0777
</blockquote>

<p>Now, remove the # from the beginning of the line. This tells MT that you want to use a setting other than the default. </li><br />
<li>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:</p>

<blockquote> 
HTMLPerms 0755
</blockquote>

<p>Now save the edited version of <code>mt.cfg</code> and upload to your server, replacing the old copy. Remember to do this in ASCII mode.</li><br />
<li>Rebuild your entire site.</li><br />
<li>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 <code>.php</code> files generated by MT, and then rebuild. The new copies should now have the correct permissions.</li><br />
 </ol></p>

<p><b>Links:</b><br />
<a href="http://www.learningmovabletype.com/archives/000484using_php_and_mt_includes.php">Using PHP and MT Includes</a></p>]]>
   </content>
</entry>

</feed>

