Redirecting Urls in .htaccess

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.

Advertisements

13 thoughts on “Redirecting Urls in .htaccess

  1. Hmm this information is great, but I have encountered a problem. I am wanting to all traffic that comes to /index.htm to just appear as/ so I tried this but it doesn’t work?

    RewriteEngine On
    Options +FollowSymLinks
    RewriteCond %{THE_REQUEST} ^[A-Z]{3, 9}\ /.*index\.htm\ HTTP/
    RewriteRule ^index\.htm /$1 [R-301,L]

    Sorry to be painful but is this correct?

    Like

  2. Thank you! I looked forever to find these straightforward instructions. Amazing the convoluted code so many people want to advise for simple solutions.

    Like

  3. I am currently using the following permalink structure and am looking to change it through a 301 redirect but cannot find the code that works.

    Currently permalink: /%year%/%monthnum%/%postname%.html
    Future permalink: /%postname%

    Can someone help me?

    Like

  4. Thanks for this – really useful.
    New challenge I’m faced with: I have changed a WordPress page name and permalink and this affects links to it of course.
    Was domain.com/pagename/subpage
    Now domain.com/new-pagename/subpage

    Do you know how I can redirect /pagename/* to /new-pagename/* ??

    Thanks for your help.
    Dan

    Like

  5. Responding to Shannon – SKS Designs

    I know it’s a little late to answer, but can serve others like me who came here looking for this answer.

    I managed to rewrite, testing a few times until you can actually.

    follows:

    Currently permalink: /%year%/%monthnum%/%postname%.html
    Future permalink: /%postname%

    RewriteRule /([^/]*)/([^/]*)/([^/]*)/$ com/$2 [L,R=301]

    (or try $3)

    Like

  6. I wish to redirect the old website to the new one. But I have many many pages. And it’s an old HTML to a wordpress. I have ALOT of pages to redirect…over 100.
    I have the start wordpress code in the htaccess. Followed by the exact redirect 301 that you clearly state here. But it keeps failing with a 500 error. Any suggestions?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s