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.
Redirect One Web Page to Another
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.
Redirect One Domain to Another
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.
Redirect One File Extension to Another
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.
Redirect domain.com to http://www.domain.com
Example Below…
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://http://www.domain.com/$1 [L,R=301]
The above code will 301 redirect domain.com to http://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.
Redirect domain.com/index.php to domain.com
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.
Redirect domain.com to domain.com/folder/
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.
Leave a comment