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.


Posted

in

by

Comments

15 responses to “Redirecting Urls in .htaccess”

  1. Introspectre Avatar

    Wow! Just the information I was looking for.. Thanks!

    Like

  2. Jess Avatar
    Jess

    Nice, very helpful. Thanks!

    Like

  3. Two Socks Avatar

    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?

    Liked by 1 person

  4. Frank Avatar
    Frank

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

    Like

  5. […] line and the old site would only forward to the homepage of the new site. Luckily, we found this article which showed the importance of the slash […]

    Like

  6. Shannon - SKS Designs Avatar

    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

  7. John - eUnlimited Avatar

    Thanks for the coded. I am not so tech savy.

    I want www blog url to 301 redirect to non www address.

    How would I do that?

    Thanks

    Like

  8. Shannon Avatar

    Redirect 301 /webpage.html http://www.domain.com/newwebpage.html throws a Internal Server Error – 500 on my site… Probably not a universal solution…

    Like

  9. dscheidig Avatar
    dscheidig

    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

  10. Waseem Avatar

    Thanks a bunch, it saved my day :=)

    Like

  11. Peder Allne Avatar
    Peder Allne

    superb code..had to redirect *.domain.com to domain.com/folder , used the above 301 redirect code with slight modification :

    RewriteEngine On
    RewriteRule ^$ http://domain.com/folder/ [R=301]

    Thanks v.much….

    Like

  12. Wellington V. Santos Avatar

    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

  13. Andrea Avatar

    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

  14. arkanaridwan Avatar

    Help !!
    How these codes can walk in .htaccess wordpress ??
    RewriteRule ^sitemap/(.*).xml$ sitemap.php/$1.xml [L]

    Like

  15. Hanna Avatar
    Hanna

    I need to note that “Redirect 301” can be good for individual files, but better not to use it for individual pages. For example:

    Redirect 301 /oldpage /newpage

    It will redirect all old suburls to new suburls. In particular it will redirect /oldpage/page1 to /newpage/page1 Therefore better to use RedirectMatch (docs here https://docs.oracle.com/cd/B14099_19/web.1012/q20206/mod/mod_alias.html ) or RewriteRule (Generator here https://www.301-redirect.online/htaccess-301-redirect-rewrite-rule-generator )

    Like

Leave a comment