The .htaccess file made easy for beginners
In this short guide I will show you how to create a .htaccess file and use it for redirects and setting up error pages. First of all, you should only be using a .htaccess file if your site is running on an Linux Apache server. If you don't know what kind of server you're using it is easy to find out.
Find out if your server is Apache here
When you have stated that your server is an Apache server you should look for your .htaccess file. It should be placed in your root folder (the same folder as your index file). If not, it's time for you to create one. The .htaccess file should always be named ".htaccess", not "htaccess.txt" or ".htaccess.html".This can be in any program from WordPad to Dreamweaver. The .htaccess file is now a blank page to put in your root folder. It is in this file you type in the commands of redirections.
Create a custom 404 page
The 404-page is the “page not found” page shown when following broken links or when visitors type in URL. If you customize it and write a message like, “Sorry, but the page you’re looking for cannot be found, please go to our navigation page”, you can lead misguided visitors back to you site again. You can also put a search box on the 404 error page, an easy way can be to use one from Google. The search bar is a part of the Google AdSense program, you will get access to this if you join Google AdSense.
Save the file as 404.html or whatever you want to name it and upload it to your server. Next thing to do is to open your .htaccess file. Now just type in this single wherever you want in the .htacces file:
ErrorDocument 404 http://www.yourdomain.com/404.html
Replace "yourdomain" with your real domain name and "404.html" with your real 404 file. The "404.html" file that we created will now be shown as our error page. This can be done with 400 (bad request), 401 (authentication required), 403 (forbidden) and 500 (server error) too.
Redirecting pages using 301 redirect
Google recommend you to do a 301 redirect if you move a page to a new place. The number 301 is a html status code and means moved permanently. You should do a 301 redirection when you for example have made a new design for any of your pages and changed the filename for it. If you don't want to remove the old one and loose search engine rankings for it, it's highly recommended that you use the 301 redirect. To do this u simply add a new line to your .htaccess file. The line should look like this:
Redirect 301 /oldfolderpath/oldfile.html http://www.yourdomain.com/new.html
or
Redirect 301 /oldfile.html http://www.yourdomain.com/new.html
Replace the "old" part with your old file location and the "new" part with your new one. If there already are some code in your .htaccess file just skip a line. Don't add http://www to the "old" part.
Check if your redirections work here:
www.yourdomain.com or yourdomain.com
If you you got mod_rewrite enabled on your server you will be able to choose which URL you would like to have, www.yourdomain.com or yourdomain.com. If you don't do this, the search engines will see these different URLs a two sites and your search engine rankings will split.
How to check if mod_rewrite is enabled on your server:
Put these lines in your .htaccess file:
Options +FollowSymLinks
Redirect /seocess.html http://www.seocess.com
Upload the file go to www.yourdomain.com/seocess.html (replace yourdomain with your real domain name). If you get redirected to www.seocess.com, mod_rewrite is enabled on your server. If it’s not enabled the following code will not work for you.
Put this code in your .htaccess file if you want to redirect www to non-www:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Put this code in your .htaccess file if you want to redirect non-www to www:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Replace yourdomain with your real domain name and if you already have some lines in your .htaccess file, skip a line before you paste in this.
Warning:
Editing the .htaccess file can cause server errors if there is something wrong in the code. If you get the 500 error message, the solution is to remove the .htaccess file from the remote server temporary. If you want to avoid server errors when testing your .htaccess file, use a test server instead of the remote server.
I hope this short little guide helped you out.
blog comments powered by Disqus