That's the basic function of Sevrer Side Includes, but there are some other, more advanced things you can do as well. Here, in this step-by-step tutorial, I try to explain how you can easily insert a piece of code contained in a single txt file in all of your documents. That way, every time you modify the txt file, the changes you make will apply to every page you've inserted the code in. Sounds complicated, eh? All right, let's go!
If you
have a paid or a free host that supports Server Side Includes (there should be something about it in their Help Pages or FAQs, assuming of course they have things like that) then you can skip the first step. You should read it though to learn more about SSI itself.
STEP 1:Enabling SSI via .htaccess
- First of all, you have to find a host that supports .htaccess. Paid hosts are most likely to have this feature enabled. The host must give you access to FTP as well, because if you upload the .htaccess file by browser, it is not very likely to work.
That means: The host you are hosted in must either support SSI or .htaccess. If it supports none of the two, then it is impossible you can take advantage of SSI. So, what we are going to do now, is show you how to create the file that will enable SSI for your site. What are the downfalls?
Firstly, you should definitely ask for permission of your host before proceeding, because enabling SSI via .htaccess might be considered hacking or violation of their Terms of Use. Secondly, it might be possible that hackers can access you site, because SSI can actually open some small holes here and there.
- After finding the right host and after sorting out any problems, you need to create a file called .htaccess. Use a text editor, such as Windows Notepad, and paste the following 3 lines without writing anything else in the file:
AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
The first line tells the server that files with the extension .shtml are valid and the second parses every .shtml document for SSI commands.
This means that you have to rename all your files to .shtml to use SSI. Don't feel like doing it? Then your file should look like that:
AddType text/html .shtml
AddHandler server-parsed .shtml
AddHandler server-parsed .html
Options Indexes FollowSymLinks Includes
If you use that, though, you should be aware of the fact that every single html page will be parsed for SSI commands and this will cause a slight increase in your pages' load times. That's because SSI does some extra stuff before serving up a page. It won't be much of a problem though, because it is a very slight change. Plus, if you enable SSI in html pages, people won't realize that you have it enabled by just looking at your page's extension. That way, you can avoid being hacked through SSI, which is possible.
If you want to keep the .shtml extension and use it in your index page,(although it's a lot easier to create a html splash page that leads to a .shtml page of your choice)your .htaccess file should look like that:
DirectoryIndex index.shtml index.html
AddType text/html .shtml
AddHandler server-parsed .shtml
AddHandler server-parsed .html
Options Indexes FollowSymLinks Includes
Then the server will try to load index.shtml as your default page and if it doesn't find it, it will load index.html
Then save the file as .htaccess(note the dot at the front).Most likely, you'll have to name it .htaccess.txt, but you can rename it later using the FTP.
- Now that you have created the file, you need to upload it using the FTP. The .htaccess.txt file has to be renamed to .htaccess after transferring. You should transfer it in ASCII mode(although it might work in BINARY mode as well) to your root directory. Then the file will disappear. Don't worry if you can't find it, because it won't show up in most FTP clients. That's because files beginning with a dot are considered as secure.Smart FTP will show this file though. How can you rename it then? Try to configure your program’s settings, or use Windows FTP. Click here for a tutorial.
STEP 2:Placing the code in your page
- You need to insert this little bit of code in every page, in the place you want to add the file that you want to show up in most or all of your pages.(Note the empty space after the quotes)In order to make it unable of infecting my page, I added an extra "a" between the two -'s. You need to remove it to make it work correctly.
You need to replace yourfilename.txt with your own file's name of course. When you edit the file, the changes will apply to every page that has the code above inserted in it. Apart from .txt files, you can also use .js files or anything else you'd wish. You can insert the SSI code more than once in a page-I use SSI for my navigation menu, my header, my footer and my copyright notice. Just be sure not to misspell the name of the file you want to insert, because you'll end up with "An error occurred while processing this directive" or something like that in the place you wanted to put the file. Now about what you can put in the .txt file, it is up to you. It is just a normal .html document.SSI will NOT work offline
- That's it, you should have Server Side Includes enabled and the files of your choice should appear in every page you want them to do so!
I hope this tutorial will help you enable SSI and use it for your own advantage. If for any reason, you feel this tutorial is unhelpful and need further advice,
drop me an e-mail.
|