This is the third part of a series discussing how to use 301 redirects in an .htaccess file in order to resolve 404 errors. In this part we discuss the syntax of an .htaccess file and look at the various parts of a 301 Redirect directive.
So then htaccess file has relatively simple syntax. So we’re going to open up our htaccess file that we just created and let’s see, websites… and you can see it’s blank. The first thing to say is that comments start off with a pound sign. And so, if you have one of these that’s created by WordPress, it’ll generally start off by begin WordPress here. We’re going to say begin 301 redirect.
Now the next thing to know is that this is intended to be use or commands are intended to be on one line. So a command is not going to you know, wrap anything like that. In fact, if you’re using a text editor that’s not like NetBeans, you want to make sure that it does not have a text wrap function operating because that will add a special code to the file which will make apache choke. So you definitely don’t want that there.
So most commands in the htaccess are meant only to be placed on one line and then pass to files or directories are specified from the server root, not from where the htaccess file is located but from the server root. Now in this point… I mean, in this case, the htaccess file is in the server root but as you’ll learn in a couple of minutes, they can also be located elsewhere.
So in fact, here it is. Each directory in a site can have its own htaccess file and there’s a specific hierarchy to these htaccess files. Each4 directory in a site can have its own htaccess file and the htaccess file only affects the directory that it’s in plus those directories that are contained inside of that directory or those nested directories.
So if you have for example, an htaccess file as it’s relatively common, really… if you have htaccess file in wp content plugins and perhaps, one of the plugins over here might have a… oh, I’ve lost my synchronization. Oh no, I haven’t. Yeah, I have. Oh no, it’s off the screen, pardon me. There we go. For example, database backup actually does have its own htaccess file in it and that htaccess file will only affect this folder and everything that’s inside of that folder.
Now the closes htaccess file governs the behavior. So if there is a function that is being called from that database backup plugin then that htaccess file is the closes htaccess file to the command. And so, if there is a conflict between that htaccess file and the htaccess file in your root directory then the closes one will prevail in terms of a conflict.
However, if there’s not a conflict, the directives from the root directory will pass all the way through. So this htaccess file that’s in the subsequent directory doesn’t block the commands. It just supercedes the commands if there is a conflict between the commands. So the closest htaccess file governs the behavior.
Okay so now what we’re going to do is we’re going to actually create this 301 redirect. And the 301 redirect has 3 parts to it. It has the directive which is redirect 301. It has the original location of the file or directory relative to the root. So there’s your root and then old directory, old page .htm, that is an example, right? And then it has the new location. And now, this new location can be anywhere on the web so you have to start off with the http colon and give the full and complete address URL to the location that you want it to forward to.
Now it’s important to know that each part is separated by a single space. So that is one space and that is one space and essentially nothing else. And these locations, they’re shown here as if they are files but they could also be directories. So you could change from directory old to directory new over here instead of from old page to new page. And if you had old page htm and old page htm over here with inside the old directory and inside the new directory… if you just reroute the… or redirect the directories, you don’t have to reference the pages. So that’s a way to make your redirect apply itself to lots of different pages.