I Turned 404 into 200 With a Tiny Bit of php – Part 2
- I Turned 404 into 200 With a Tiny Bit of php – Part 1
- I Turned 404 into 200 With a Tiny Bit of php – Part 2
|
Welcome to Part Two of this series. In Part One we looked at the situation that I was facing, having moved to a dedicated server from a shared-platform server. Being a noob as far as Linux is concerned, I followed leads from other people who were more experienced in using Linux, (Although one of them did in fact confess that, although he used Linux more than anything else at work, his main area of proficiency was, in fact, Windows.) which ended in a dead end. Not to be outdone I eventually discovered a blog post from February 2008 which appeared to contain part of an interesting line of php that turned out to be the answer to my problem. In Part Two, which you’re reading now, we’re going to look at that line of php, as well as add it to our respective blogs to prevent unnecessary 404 errors.
First, though, from my experience, although somewhat limited, I suggest that only bloggers who are hosted on a dedicated server use this code: I don’t see why it would be necessary to run on a shared-platform server. If you are on a shared-platform; that is sharing a server at your hosting company’s depot with others unknown, then your web-host’s engineers will normally be tending to the server and keeping it problem-free. (Which, in my case, is why it costs more to host 4 individual websites on separate domains on shared-platform servers than it does to host those 4 domains on a single dedicated server: Because the engineers will only fix hardware faults and occasionally give limited advice on dedicated servers.) If you get unexpected 404s on a shared-platform server, phone customer support and they’ll ask the engineers to put it right. – Only use this code if you have no other alternative. Note 1: – Which brings me on to thinking: There must be some setting somewhere in the server which will prevent the 404 issue from occurring. The engineers can sort it on shared-platform servers; so why can’t I on a dedicated server? The answer to that is; a) I’m a technician. – Engineers are much better trained and have a wealth of experience. b) I’m a trained electronics technician, rather than a trained computer technician. c) Er, read on: You’ll see that I have sorted it out; but not in the server settings (Linux) themselves; rather, on a third-party application, namely WordPress, that is on the server, using php. OK; so first, as promised, I’ll reveal where I found this code and give credit: The site is called “The Moonlight Blog”. I’ve linked the name to the relevant page. As you’ll see, the writer gives this information: -
Note the php. When rebuilt by myself it looks like: - <?php header("Status: 200 OK"); Yes I did say a single line of php; well it sort of is; it just works if it’s typed like that; so that’s how I’m presenting it. “That was the fruit of a week’s work?” you ask; bemused. In itself that was the fruit of a few minutes work; it just took me a week to find it, as well as many other things besides. Note 2: In Part One I stated that the author’s php notation was incomplete; that’s true and then again not true: You see if you’re working with php all the time you know what goes where, and you come to expect a pattern. – so much so in fact that you only need write the information that’s not obvious to the trained eye. header(“Status: 200 OK”); is the bit that the developer needs. The developer knows that it’s php because the semi-colon shows that it’s the end of the statement. The machine won’t know that it’s php because the machine hasn’t been told that it’s php. The bit that I added tells the machine that it’s php; otherwise, to the experienced developer, it’s simply not required.
OK so we have our line of php code; written so that the machine can understand it. Where do I put it? Well; since it’s an instruction for the header of a page to return a html 200 code when executed; it appears logical to put it in the header.php file in the theme folder; located at ~\wp-content\themes\<name of your theme>\header.php. if you have more than one header folder; for example: page_header.php and post_header.php, put it in all of the header files. Where exactly do I put it in the header.php file(s)? Right at the very top; above all the other text. Why? Because it works. How do I add it to my header.php file? There are 2 ways to do this: The first is possibly the easiest, and is from inside the WordPress GUI itself: - (This method will only add it to the currently active theme installed on your WordPress installation.) Click on the arrow on the Appearance tab and click on Editor. On the page that follows, look on the right-hand-side of the screen and click on Header (header.php). In the text editor in the centre of the screen, type or copy & paste the php from this article above all the other text. You might have to click at the start of the first line and press return to create a clear line at the very top before you insert it. Below the text editor window you’ll see a blue button with white writing on it “Update File”. Click it and wait for the function to execute. – That’s it. The other way of doing it is to open up your FTP client and navigate to the wp-content folder on your server. On an Ubuntu server the path could look something like /home/default/yourdomain/user/htdocs/wp-content (Various servers/hosting companies may vary.) Inside that folder you’ll find a folder called “plugins”. Open that and you’ll see a folder with the name of your current theme. Open that folder and download the header.php file to your local computer. Use a text editor program to open the file and insert the php from this article at the very top of all the other text. Close and save. Use your ftp client to overwrite the existing header.php file on your server with the edited file. Do the same with any other themes you may have loaded as you wish. -It’s as easy as that: The word “easy” depending upon your previous experience. Just to recap: We’ve added some additional php to our header.php file in our header.php file in our WordPress blog to instruct the page headers to return a html 200 – OK when executed. This should stop them returning a html 404- not found error. The server then has no choice but to execute the other php on the page in question and serve the product of that execution to the requesting machine. - And that’s about all there is to say. This php did the trick for me. Why not let me and all my readers know whether or not it did the trick for you too? Leave a comment. Leave a comment also if you have anything further to add. I’m not a php expert, whereas you might be though; so do feel free to add something. That’s all. - And now; some adverts: -
|
View Comments
Sorry, the comment form is closed at this time.
Did you like this post? If so then why not join the Kkomp.com - Beyond community and get a free pdf report?
Use the mini-form below to enter a name + email address to receive your pdf report download location, as well as extra mailings:-
![]() |
Please subscribe to my RSS feed. Click here.
- Confused about RSS? This short video should put your mind at rest: -
.flv (flash) format. (Real Player) - 9.185MB ~ OR ~ .wmv format.(Windows Media Player) - 11.330MB










No comments yet.