Converting SSI / SHTM Sites to PHP in Minutes – Guide

I was recently lucky enough to purchase a local website hosting company, and I quickly discovered that about 30% of the websites used .shtm files. These SSI files used “HTML Include” directives to load additional content to each page, such as the header and footer.

And they didn’t function – at all – on my servers.

As it turns out, SSI is deprecated and not supported at all by Firefox and Google will stop supporting it by April 2020. So what was I to do?

The solution? Convert the whole site to PHP!

While I had never worked with SSI before, I am extremely comfortable working with PHP since we work almost exclusively with WordPress websites. If the entire purpose of the .shtm files was to include other files, PHP could do that easily.

Tired of dealing with your slow WordPress website? Enter in your email address here to receive the secret to lightning-fast load speeds overnight.

Secret to Faster Website Signup

I assumed that there would be simple guides I could follow to convert a site into PHP with minimal effort.

I was wrong.

Luckily for you, that guide exists now, and you’re currently reading it!

You see, I spent the better part of a day figuring out how I could automate the whole process. It seemed to me that all we would really need to do is change the file extension of each page, add some PHP code to make it official, swap out the SSI directives with PHP commands, maybe set up some redirects and boom! Done.

As it turns out, it is, in fact, possible to do that. It took a while but I was able to create some batch files and regex replaces that would take care of everything for us.

So broadly, I have created batch files and more that will accomplish the following things:

  1. Rename each of the site files and add the necessary PHP tags to the beginning and end of every file
  2. Replace the SSI includes with PHP include commands
  3. Replace all site links to .shtm files with links to their PHP versions (Not required if you do step 4, but better for SEO)
  4. Set up permanent redirects from the old .shtm pages to their PHP versions

The first step happens all in a batch file which means it’s really easy to do. Just place the file and run it.

Steps 2 and 3 happen in Notepad++ with some basic search and replacing.

Finally, step 4 is simply an .htaccess file. But every step is actually pretty easy!

WARNING! I strongly advise you to back up your entire website before attempting any of this. It is possible that things won’t work quite the same with your code or system, and you’ll need to be able to start over if something goes wrong.

Batch File to convert every .shtm file to PHP at once

This is the beautiful part. One simple batch file can automate almost all of this. It works by changing the file extension of every file in your folder and subfolders that ends with .shtm and converting them to end in .php. It then adds the code <?php ?> to the beginning and end of each document, which is required for them to be functioning PHP files.

So how do you use it? Easy! You’ll need to be using Windows for this. I know it works on Windows 10 and probably 8, but I haven’t tested with other versions. Sometimes commands are slightly different so you may need to tweak it if you have an older version.

All you need to do is create a file in your local site root called “convert.bat”. Or whatever you want to call it, as long as it ends in “.bat”. Now, copy the following code into it:

Now, you just need to run it! After it is done, test out some files and make sure they all changed. it’s magic!

Note that if your files use the .shtml extension instead of .shtm, you’ll need to modify line #1 of this script to take care of those. You may need to alter future steps as well as needed.

For a breakdown of each line in plain English, here’s what each step does:

  1. For every file ending in .shtm do the following: (Starts a loop)
  2. Create a new file, temp.txt that has the contents of the original file
  3. Recreate a file with the original file name, but blank
  4. Add the following code to the beginning of it: <?php ?>
  5. Place the contents of temp.txt into that file right after what we just added
  6. Add a line break to it
  7. Add the following code to the end of the file: <?php ?>
  8. Rename the file to replace the extension with .php
  9. Delete our temp file

It’s probably not important to understand all of this unless you need to modify it for your purposes, but I think it’s good to understand how these things work.

Replace the SSI includes with PHP include commands

So this is the part where we are actually recreating the functionality and purpose of the SSI code to begin with.

For me, I was fortunate: the commands for all the sites were formatted the same way and performed the same task. If yours vary, you may need to modify this code a bit.

Basically, every command looked like this:

The paths varied, of course, but other than that, this didn’t really change much.

So here’s how we do it.

  1. Open up Notepad++ (Free software, you can download it!)
  2. Press ctrl+f to open up the find window
  3. Click the “Find in Files” tab
  4. Under “Find what”, enter in the following parameters: <!--#include(.*?)"(.*?)"-->
  5. Under “Replace with”, enter in this: <?php include '\2' ?>
  6. Leave filters as *.*
  7. For Directory, select your website root directory (again, make sure it is backed up first)
  8. Select “Regular expression” from the “Search Mode” area and click “Replace in Files”

It should take a moment to process and then let you know how many files it has changed. Hopefully it covers most of them! (Assuming they have SSI includes)

If you have .shtml files as well, make sure you run the same thing but with the ā€œlā€ at the end.

Replace all links to .shtm files with links to the PHP versions

This step isn’t absolutely necessary if you follow the next step, but it’s still a good idea. This will make sure all of your pages get indexed properly and will help a bit with SEO. I highly recommend doing it!

We’re going to use Notepad++ for this again, and basically do the same thing as in the last step but with different search and replace parameters. This time we’ll use:

Find what: (.*?)\.shtml

Replace with: \1.php

Click “Replace in Files” and you should be done! Anywhere an .shtm file is mentioned, it will replace it with a link to the PHP version.

Then do the same thing but with .shtm files. You have to do it in this order, otherwise you end up with links to “.phpl” files which obviously won’t exist.

Find what: (.*?)\.shtml

Replace with: \1.php

Set up permanent redirects from the old .shtm pages to their PHP versions

This step is critical. You will have major SEO problems and could lose all traffic for a while if you don’t perform his.

What this does is ensure that anybody who tries to visit the old .shtm pages will be redirected to the new version. It also means that any backlinks to your old pages will still work and pass both traffic and link juice to you.

For this, just create a file named “.htaccess” in the site root. We’re going to paste the following redirect code in. If it already exists, just add this code to the end of the file.

If you’d like more information on how this section works, view our other blog post on redirecting all files with a given extension to another extension.

This should permanently redirect visitors to the new files. You’re all set!

Potentially necessary last step: remove leading slashes from URLs

Many shtml links may include a leading slash before Urls, for example: “/folder/filename.html”.

For some reason PHP doesn’t like that in my experience. So we’ll need to do one last step to remove those, but only for PHP include URLs.

  1. Open up the search and replace screen to “Find in Files” in filezilla just like before.
  2. For “Find what”, enter this: include '/
  3. For “Replace with”, enter this: include '
  4. This time, make sure “Normal” is selected as the search mode, NOT “Regular expression”.
  5. Make sure you’re still selected in your directory and then do “Replace in Files”.

And with that, you should have completed all of the necessary steps to converting your .shtm / SSI site to PHP. Now it will be much more future-proof, should work in any browser, and will also be much more flexible going forward.

I’m not really sure why there are still sites using this outdated method but hopefully I’ve helped a few people out who need to convert their SSI site to something that will keep working. PHP is so much better in every way!

Of course, my ideal recommendation would be to upgrade to something like WordPress, but this method should work quite nicely for those who just need to keep the same one working with minimal effort.

If you have any questions or need any help, feel free to reach out with the form below! We work on any kind of website even though we specialize in WordPress. We also provide lightning-fast website hosting to make sure everything comes up quickly.

Good luck!

 

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

Leave a Reply

Your email address will not be published. Required fields are marked *