Some Good Official Tutorials
- Apache Docs Tutorial: .htaccess files
- Apache Docs: List of available Directives
- Apache Docs Tutorial: mod_rewrite Reference
- Apache Wiki: Various configuration recipes
Sample .htaccess file from htaccess Google Group
Sample .htaccess file from htaccess Google Group
This article will teach you how to use the .htaccess (hypertext access) file to properly redirect web pages or domain names. Using htaccess redirect is the best way to redirect when it comes to search engine optimization.
The .htaccess file is actually the name of Apache’s directory-level configuration file. The code displayed in the .htaccess file is the directives for that directory and its sub-directories. These methods cannot be used on a windows web server, and if the file doesn’t exist on your linux hosting account you can create one in notepad.
The reason that 301 redirects are the best method is they are instant. The web server will check the htaccess file before loading the web page, so if its 301 redirected it will instantly show that web page. Search engine’s also use 301 redirects to find where new web pages have moved to and properly pass over any link value or search results position for that web page.
Below are specific htaccess codes you can use to properly 301 redirect for that specific situation. Always make sure to back-up your current .htaccess file before applying any changes and make sure to upload it in ASCII format. Make sure when you copy and paste the below code below to change domain.com to your actual domain name.
Example: Redirect 301 /webpage.html http://www.domain.com/newwebpage.html
The above code is used to 301 redirect one web page to another. The code in red is the old web page and the code in green is the URL to the new web page. You can add directories to the code in red if thats where the web page is located. This should be added to the root directory .htaccess file.
Example: Redirect 301 / http://www.domain.com
The above code is used to 301 redirect one domain name to another. The code in red is where you place the domain name the current domain is being redirected to. This must be inserted in the root directory .htaccess file for the domain name thats being redirected.
Example: RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
The above code is used to 301 redirect one file extension to another. This is useful if you just change your file extensions for your web pages and keep the same URL structure. The code in red is the file extension you want to redirect. The code in green is the domain name where the web pages are located. The code in blue is the actual file extension your redirecting to. The above code redirects all *.html requests to *.php and should be inserted into your root directories .htaccess file.
Example Below…
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The above code will 301 redirect domain.com to www.domain.com. The code in red should be your domain name with the www in next to the ^ sign. The code in below is your domain name with the www prefix attached to it. This should be done with all sites that want to have their on-page SEO as optimal as possible. This needs to be installed in your root directories htaccess file.
Example Below…
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3, 9}\ /.*index\.php\ HTTP/
RewriteRule ^index\.php /$1 [R-301,L]
The above code will 301 redirect any index.php request to the root directory it resides in. This means domain.com/index.php will redirect to domain.com, the code in red should be the default index page of your websit; make sure to leave the \ inside of the code if you need to change the default page. This needs to be uploaded to your root directories htaccess file.
Example Below…
RewriteEngine On
RewriteRule ^$ /blog/ [R=301]
The above code will 301 redirect any domain.com requests to a folder. This is good for people who make the mistake of install their blog or ecommerce backend in a folder inside the root directory. The code in red should be the name of the folder the root domain should be forwarded too. This needs to be inserted in the htaccess file in your root directory.
Editing the .htaccess file can make your website inaccessible if done incorrectly. Always make sure you have a back-up of the current version of the .htaccess file, to go back to a working version.
.htaccess is a file on Apache servers. It’s a very powerful tool, but often requires rather detailed technical knowledge to edit. Thankfully it’s not that difficult for the purpose of redirecting pages on server level.
Step 1: Check for existing .htaccess
First you need to find out whether you have a .htaccess file already. Start your FTP-program (make sure your program shows the .htaccess file), browse to the root of your domain and look for it (it’s usually on top of the list). If it’s there, download it. Make sure to make a back-up of the current file!
Step 2: Edit it to add redirects
Open the .htaccess file in a text editor and add the following lines to the file.
# Temporary redirects for affiliate links
Redirect 302 /[subdir]/[filename] http://example.com/?id=12345
Redirect 302 /[subdir]/[filename] http://example.com/?id=34567
# End of affiliate redirects
Now replace /[subdir]/[filename] with the right link, and replace http://example.com/?id=12345 with the address you want to send your visitors to when they click the link.
Make sure you use a temporary redirect. The status code 302 tells the other server that the page is redirected temporary, and that they should not update their addresses to the new address.
For example, for my e-junkie affiliate link this looks like this:
Redirect 302 /go/e-junkie.php http://www.e-junkie.com/?r=11261
Step 3: Save and upload
Upload and overwrite the old .htaccess file on the server.
Again make sure you have a back-up of the old version before you do this!
Step 4: Link to it.
That’s it, now use that link instead of the original link. It doesn’t even matter if there’s a file or not on the original location.
Again, use caution with this file. Make sure you have a copy of a working version and ftp-access to your server in case things go wrong. An error in the .htaccess file could effectively deny you access to the WordPress Admin interface. In which case you’ll have to replace the erroneous file with the back-up through FTP.