I'm just an average webmaster. Admittedly, I'm better at the technical aspects than the creative side, but not much better! :) And I managed to learn enough about this stuff to use it. So can you! Let's start with something easy.
The Quick and Dirty Solution The Real Deal Caution! Some hosts hide system files such as .htaccess. Since overwriting an .htaccess file can take your site down, don't assume it's not there simply because you don't see it. Ask your host! What's An .htaccess File? The "RewriteEngine" portion is the directive and "on" is a parameter that describes what "RewriteEngine" should do. More on this directive in a minute. The .htaccess file usually lives it the root directory of a site and allows each site to uniquely configure how Apache delivers its content. Its directives apply to the entire site, but subdirectories can contain their own .htaccess and it applies to this sub and all of its subs and so on, down thru all of your sub sub sub sub subdirectories... You could have a different .htaccess in every sudirectory and make each sub behave a little differently. You get the idea. Your site may have had an .htaccess from day one, or not, depends on how your host configured Apache. If it did, MAKE A BACK UP! Treat the backup as read only! Do NOT edit, delete, revise, bend, fold, staple, or mutilate any of the lines it contains! Ever! Changing something critical can take your site down. Until you know exactly what you are doing, ADD code to the copied file or revise your OWN code, but keep your paws off of the rest of the it! And don't fool with the back up! If you didn't have an .htaccess when you acquired hosting (and still don't) and your host said you can use mod_alias or mod_rewrite, we'll make one. Not just yet though. Mod_alias Or Mod_rewrite? Let's assume we have page1.html and page2.html in the root of our site and we want to redirect all visits from page1 to page2. Either module can handle it but let's keep it as simple as we can. Mod_alias is perfect for this job! For example: "RedirectMatch" is the directive and the remainder of the line contains parameters that tell the directive what to match and where to send the requests that match. The first parameter, "page1\.html" is a "pattern" of characters to match. The second parameter, "page2.html" is the URL to redirect to. Not so fast! "Pattern" of characters? Yup. In the *nix world this is called a Regular Expression. If you've used "wildcards" with M$ DOS or Windoze, somefile.* or some?.exe for example, you already understand the concept. But regex (regular expressions) are much more powerful. And complex. A Few Words About Regex I know enough about regex to get the job done (after a few false starts!), but I'm far from a master. I'll explain each example in this post as best I can, but YOU will need to learn more about this on your own. We have a few regex masters that frequent this forum that are usually willing to help. Here are a few good tutorials to get you started: Back To Our First Redirect Example We could have used a different mod_alias directive to solve THIS problem. But RedirectMatch introduces concepts that are also used by mod_rewrite so... The Apache mod_alias docs are at http://www.webmasterworld.com/red.cgi?f=92&d=82&url=http://httpd.apache.org/docs/mod/mod_alias.html The "pattern" portion of our example (page1\.html) is pretty simple. It says to match the exact string "page1.html", not page1a.html or page1.shtml. The backslash after the "1" and before the period in our pattern is an escape character. Regex uses a few "special" characters to describe which characters and how many characters to match. Unforunately, the period in our example is one of those special regex characters. So we use a backslash to "escape the period" and tell regex that this is indeed a period that we want to match, and not a regex "special" character. What Will This Redirect Do To My Primo SE Listings? The square brackets surrounding "status" indicate that this is an optional parameter (a value that MAY be included) and is not required. When used, it should be included WITHOUT the square brackets, exactly as shown in our example below. The status parameter may contain any of four values, "permanent", "temp", "seeother", or "gone". For example: This example performs our redirect and returns a "301 moved permanently" status code with page2.html. From this status code, SEs will know that this URL has changed, and usually update their index in response. Voila! Using "temp" as the status parameter will return a "302 moved temporarily" header and it's the default returned if no status parameter is included in your directive. Read the darn manual if you want to know more about server headers and status codes! The Big Gun - Mod-rewrite! My understanding of the first directive, "Options" and it's parameters, is limited. But for this post, I've assumed that your site is remotely hosted. This directive instructs Apache to follow symbolic links within your site. Symbolic links are "abbreviated nicknames" for things within your site and are usually disabled by default. Since mod_rewrite relies on them, we must turn them on. The "RewriteEngine on" directive does exactly what it says. Mod_rewrite is normally disabled by default and this directive enables the processing of subsequent mod_rewrite directives. Someday, it might be handy to insert RewriteEngine "offs" and "ons" at different points in a complex set of rules to help isolate which rules are failing. In this example, we have a caret at the beginning of the pattern, and a dollar sign at the end. These are regex special characters called anchors. The caret tells regex to begin looking for a match with the character that immediately follows it, in this case a "p". The dollar sign anchor tells regex that this is the end of the string we want to match. In our simple examples, "page1\.html" and "^page1\.html$" are interchangable expressions and match the same string, however, "page1\.html" matches any string containing "page1.html" (apage1.html for example) anywhere in the URL, but "^page1\.html$" matches only a string which is exactly equal to "page1.html". In a more complex redirect, anchors (and other special regex characters) are often essential. In our example, we also have an "[R=301,L]". These are called flags in mod_rewrite and they're optional parameters. "R=301" instructs Apache to return a 301 status code with the delivered page and, when not included as in [R,L], defaults to 302. Unlike mod_alias, mod_rewrite can return any status code that you specify in the 300-400 range and it REQUIRES the square brackets surrounding the flag, as in our example. The "L" flag tells Apache that this is the last rule that it needs to process. It's not required in our simple example but, as your rules grow in complexity, it will become very useful. As your understanding of mod_rewrite deepens, you may add conditions to your rules (RewriteCond directive) and the "L" flag will tell Apache that mod_rewrite can quit processing these conditions after performing the rewrite, IF the RewriteRule pattern is matched. Experts suggest that you get in the habit of including the "L" flag with every RewriteRule to avoid unpleasant surprises. As you gain experience, you may encounter situations where it's not needed. Experts assure me that these are very rare. The Apache docs for mod_rewrite are at http://www.webmasterworld.com/red.cgi?f=92&d=82&url=http://httpd.apache.org/docs/mod/mod_rewrite.html and a variety of "real world" rewrite examples are included in the Apache URL Rewriting Guide. It's written by Ralf S. Engelschall, the genius who created mod_rewrite, and you can find it at http://www.webmasterworld.com/red.cgi?f=92&d=82&url=http://httpd.apache.org/docs/misc/rewriteguide.html Build An .htaccess File One last point. If you have everything working as described in the last paragraph, rename or delete page1.html, then surf to it again. Mod_rewrite can redirect from non-existent URLs ("phantom pages") to existing ones. This could be very useful for sites that create pages dynamically and often have search engine hostile URLs. In Conclusion (finally!) |