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: -
|
A Geek’s Toolkit Supplement: Loaded USB Drive
|
Being a geekette; that is a female computer designer/builder, as well as a blogger, I often get asked by my girlfriends (And some of their boyfriends too – Shh!) to pop over for a cuppa and attend to some problem they’re having with their computer. Most of these problems turn out to be software-related, so it’s always a good idea to come prepared – Although I can download most of what I need from the internet, sometimes their internet connection is not working for whatever reason, and more than one is on dial-up – Which can make downloading a program a 2-hour job in itself. To combat this I have a 1Gb USB flash drive with most of anything I might need on it; plus loads of links to anything useful on the internet itself that might be useful which I haven’t bothered to include because I probably won’t need it. |
Buy “WordPress on Crack” – Build your own WordPress plugins: Click Here! |
|
I pop the cord from which the USB stick hangs around my neck, and go to sort out their issues. I mainly have program installers in the form of .exe files on the USB drive: These are always useful. I also am able, in a worst-case scenario, to provide them with a temporary internet connection via my mobile device, for which I also carry a USB bluetooth dongle to plug into their computer on the rare occasions where it is necessary; such as an instance a year plus ago where a friend somehow lost the driver for their network interface device and was unable to communicate with their router as a result: I removed the old driver which was faulty but obviously didn’t have that particular driver on the USB device; so I set up a temporary internet connection via my mobile phone/bluetooth and downloaded a new driver, installed it – Target neutralised. The items I have on my USB drive are listed below: I’m sharing this with you as I’m assuming that as a geek you too get called out to similar situations; but being unable to fix it on the spot you end up taking their computer home, doing the work in your spare time, and returning their computer at a later date; which causes inconvenience to both parties. |
Advertisment:Fire Your Computer Guy or Girl!A computer technician spills the beans and makes available the knowledge he has charged clients hundreds in service fees for.To find out moreCLICK HERE |
|
The files I carry around on my USB device are as follows: 1. AVG Anti-virus.exe installer: You’d be mightily surprised how many people don’t run an anti-virus program! They might find that their computer slows down with use; and, not realising that it is caused by the registry getting cluttered with crap among other things, they start removing programs which they think are unnecessary in order to attempt to get the machine to speed up again, one of those “unnecessary” programs being their anti-virus program. 2.. A free anti-spyware program; such as AdAware and Spybot Search and Destroy – For the reason stated above or that they didn’t even know what spyware was etc. The latest AVG antivirus has built-in spyware-protection, but that protection is not exhaustive, so it’s always a good idea to install extra spyware protection supplemental to that. 3. Free FTP clients in .exe installer format, such as WS-FTP-LE and FileZilla: They do occasionally come in handy. 4. Diagnostic programs; such as Core Temp.exe, Diskcheckup.exe… I know; rather than listing them all here I’ll show a picture of all the icons below and to save a lot of my time I’ll let you Google for them all. Obviously the folders are of my own making: Batch Files contains some useful batch files that I wrote or copied, Dragons Websites contains the URLs of the websites of the millionaire entrepreneurs from the BBC series “Dragon’s Den”, Glint(Program) contains the .exe file of the Glint System Monitor program, KK contains pictures which I use regarding Kustom Komputa; Suppliers, Parts, and Circuits contains URLs to suppliers of computer hardware components, plus a few electronic circuits, Web Shortcuts contains hundreds of various useful URLs, Websites contains copies of all my website files from some of my various sites, WP Plugins contains some useful WordPress plugins. Some of the files, such as DSC00* are photos from my mobile phone. aports.zip shouldn’t be on there as it’s a program that contains malware. OEM Exel and OEM INXP are folders containing branding files which I add to the OS to indicate that I am supplier, builder, and maintenance tech for a particular computer. The file “Kustom Komputa” is a copy of some files from my Kustom Komputa website. WP Themes contains WordPress themes. The Folder “Self-Installing Scr” contains a number of screensavers that I created and which automatically install on the computer upon activation of the .exe file thereof. (No malware involved.) The folder Paint.net contains the program Paint.net, and the folder “Sounds” contains some alternative Windows sound effects as spoken by the Daleks from the BBC TV series “Dr Who”. There are also a number of shortcuts which you don’t normally see on any Windows desktop. These shortcuts; such as “Sound Recorder“, “Volume Control“, “Command Prompt“, “Device Manager“, and “Sleep or Hibernate” are described on this site, including the method to create your very own icon(s).
|
|
So that’s pretty much it: Carry this lot plus your own personal files around on a USB flash drive and you won’t go far wrong. HTH (‘Hope That Helps.) |









