I’ll admit htaccess does not contain the most enjoyable “directives”. Directives, a name for the syntax used to write your Apache web server configuration file can be quite confusing. If you are unsure of .htaccess and what it is here is a wonderful guide: Htaccess Made Easy
One of the most basic tasks is the 301 Redirect. During a site redesign developers often change the URL configuration to the site in order to manipulate simple things like directing users from yoursite.com/about to http://yoursite.com/about-us . But, if Google already has the /about directory indexed then old hyperlinks will cause an overflow of 404 Page not found errors. This overflow of 404’s can hurt SEO and also leave visitors staring at white screens of death.
You may need a 301 if you end up in these scenarios:
- You’ve moved your site to a new domain, and you want to make sure your old links aren’t giving users dead ends.
- You have a few domains that you want to land at the same address, like welcome.yoursite.com/ and yoursite.com/welcome
- If you are combining two separate sites and want to make sure the old links drive users to the correct corresponding pages
If you already know what a 301 is all about here are some handy tips to understand when it comes to redirects.
The three main parts:
- The Command
- The Location of the file that needs redirected
- The full* url of the location you want that request sent to
All parts are separated by a single space and should be on a single line
redirect 301 /about/management/ http://yoursite.com/management/
The previous example would move any URL requests with /about/management/ to the latter http://yoursite.com/management/
One important note: htaccess seems to have pure hatred for the beloved _ “underscore” character. In fact, htaccess treats this symbol as a concatenation character (or a word connector). So if your original redirect file contains underscores you are going to need to use some Regular Expressions to get the job done.
That’s where Redirect Match comes in. There are three parts to this directive as well.
- The command itself (RedirectMatch 301)
- The Regular Expression
- The full URL to which you want to be redirected to.
RedirectMatch 301 ^/about/management/(.*)$ http://yoursite.com/management/index.php
Another thing to note is that the RedirectMatch 301 command wants to be redirected at a file and not just some directory. You may notice that if you supply the regex with a simple directory name that it becomes appended to the end of the request URI.
In the above example any file in the management directory is going to be redirected to the management/ index page
This is because RedirectMatch is expecting a filename and not a directory.
Again, htaccess is a massive beast. I think it’s best to begin reading the htaccess docs on apaches site: Apache.org
And also, don’t forget about this awesome book: htaccess made easy