If you have ever built or managed a website, you know that site file names don’t always stay the same for whatever reason. This is when you need to add “301 Moved Permanently” rules to your .htaccess file.
This does a few things. First and foremost, it redirects your users to the new page on your site and secondly, tells the search engines that the old page no longer exists, and that the new page should replace it.
If you don’t already have one, you will need to create a .htaccess file within your site root.
You can make and edit this file right on your desktop. Depending on the OS you are using it may not let you create a file named .htaccess on your desktop. Simply name it htaccess.txt and then change it to .htaccess after you upload it to your site root.
Your Nexcess site root is in /domain.com/html right along side your index file.
First you need to turn on mod_rewrite. You only need to do this once in the .htaccess file.
## Rewrites Options +FollowSymLinks RewriteEngine On
Your hosting configuration may have +FollowSymLinks enabled at the root level. If you get a 500 error, just remove the line
and it should work.
Here are a few simple and common redirects.
Say you had a page you named kittens.html and your kittens grew up to be cats and you need anyone still going to kittens.html, to be redirected to cats.html. The direct and simple way to accomplish this would be the following:
## 7/2012 ## Kittens grew to Cats RewriteRule ^kittens.html$ /cats.html [R=301,L]
The line starting with 2 number or pound (##) characters is where you can add comments so you know what you did and why you added that rule. I use 2, you can use 1 if you like. I also add a date in my comments.
Use the php extension instead of html.
## 7/2012 Use php instead of html RewriteRule ^cats.html$ /cats.php [R=301,L]
You only need 1 .htaccess file, just add new rules on new lines. Here is an example of a .htaccess file with a few rules.
## Rewrites Options +FollowSymLinks RewriteEngine On ## 7/2012 ## Kittens grew to Cats RewriteRule ^kittens.html$ /cats.php [R=301,L] ## 7/2012 ## Molehills became mountains RewriteRule ^molehills.html$ /mountains.php [R=301,L]
I hope this was helpful in getting someone introduced to .htaccess and 301 redirects.Posted in: General