General Tips and Tricks

2014 年 6 月 28 日6510

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).

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?

    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.

    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.

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

    Follow the first 2 steps under Loading index.php by default, and get ready to edit the .htaccess file.

    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 (and possibly ) setting in mt.cfg.

    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.

    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.

    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 .php files generated by MT, and then rebuild. The new copies should now have the correct permissions.

Links:

Using PHP and MT Includes

0 0