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.
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:
- Rename each of the site files and add the necessary PHP tags to the beginning and end of every file
- Replace the SSI includes with PHP include commands
- Replace all site links to .shtm files with links to their PHP versions (Not required if you do step 4, but better for SEO)
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
:: Batch file to convert SHTM / SSI files to PHP. Retrieved from PageCrafter at https://pagecrafter.com/converting-ssi-shtm-sites-to-php-in-minutes-guide :: Follow additional steps located at that page to complete the conversion FOR /R %%A IN (*.shtm) DO ( copy "%%A" temp.txt copy /y NUL "%%A" >NUL echo.^<?php ?^>>%%A type temp.txt >>%%A echo.>>%%A echo.^<?php ?^>>>%%A ren "%%A" *.php del temp.txt ) FOR /R %%A IN (*.shtml) DO ( copy "%%A" temp.txt copy /y NUL "%%A" >NUL echo.^<?php ?^>>%%A type temp.txt >>%%A echo.>>%%A echo.^<?php ?^>>>%%A ren "%%A" *.php del temp.txt ) |
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:
- For every file ending in .shtm do the following: (Starts a loop)
- Create a new file, temp.txt that has the contents of the original file
- Recreate a file with the original file name, but blank
- Add the following code to the beginning of it: <?php ?>
- Place the contents of temp.txt into that file right after what we just added
- Add a line break to it
- Add the following code to the end of the file: <?php ?>
- Rename the file to replace the extension with .php
- Delete our temp file
The rest just does the same thing, but for .shtml files.
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:
1 |
<!--#include file = "includes/header.htm"--> |
The paths varied, of course, but other than that, this didn’t really change much.
So here’s how we do it.
- Open up Notepad++ (Free software, you can download it!)
- Press ctrl+f to open up the find window
- Click the “Find in Files” tab
- Under “Find what”, enter in the following parameters: <!--#include(.*?)"(.*?)"-->
- Under “Replace with”, enter in this: <?php include '\2' ?>
- Leave filters as *.*
- For Directory, select your website root directory (again, make sure it is backed up first)
- 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)
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: (.*?)\.shtm
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.
1 2 3 4 5 6 |
# Redirect all .shtm and .shtml files to their PHP versions # From PageCrafter at https://pagecrafter.com/converting-ssi-shtm-sites-to-php-in-minutes-guide RewriteEngine On RewriteRule ^(.*)\.shtml$ /$1.php [R=301,L] RewriteRule ^(.*)\.shtm$ /$1.php [R=301,L] |
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.
- Open up the search and replace screen to “Find in Files” in filezilla just like before.
- For “Find what”, enter this: include '/
- For “Replace with”, enter this: include '
- This time, make sure “Normal” is selected as the search mode, NOT “Regular expression”.
- 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!