Sometimes it's a necessity, whether its a 301 temporary redirect or a more permanent rewrite rule for the entire website. Here's some of the more regular ones I've used in the past.

All of these rules should go within the .htaccess file of your website, if you do not have one then it should be located where your vhosts have been setup to, typically at the root of your project.

Site wide redirects

Redirecting your site to HTTPS

If you have an SSL certificate installed on your website then it's definitely advisable for any traffic coming in at http://yoursite.com to be redirected automatically to its https equivalent, for example: https://yoursite.com. This will be a deal-breaker for SEO.

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTPS} !on
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Redirect your site to www.yoursite.com

The www. subdomain has been a staple for websites for many years, and whilst there's no real benefit in using it anymore, some people prefer it as their users may expect it. Here's how you can drive traffic coming in at http://yoursite.com to http://www.yoursite.com.

Redirecting your site to HTTPs and www
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC]
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]
</IfModule>

Redirect your site to non-www.

On the flip side, if you'd prefer to remove the unnecessary subdomain but still cover your bases incase your users try to access it, then you can do that. This will drive traffic coming in at http://www.yoursite.com to http://yoursite.com.

Redirecting your site to HTTPs and www
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www.yoursite.com$ [NC]
    RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]
</IfModule>

Combining https and www redirect rules

If you wanted to combine the above rules, then you've got one of two options depending on whether or not you want www or not.

# https and non-www.
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www.yoursite.com$ [NC]
    RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]

    RewriteCond %{HTTPS} !on
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# https and www.
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC]
    RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]

    RewriteCond %{HTTPS} !on
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Content-specific redirects

Aside from site-wide redirects, here's some more specific redirects for pages within your website. These can come in handy if you change the URL of a page or even an entire section.

Redirecting one page to another

If you've moved a page on your website or you're looking to create an alias to a different page on your website, you can use a standard 301 redirect.

Redirect 301 /old-page https://yoursite.com/new-page

To give a little explanation on that rule, let's split it up into three sections. First of all Redirect 301 specifies that we are creating a temporary redirect on our website. Next, /old-page is the URI of the page we want to redirect from, relative to our website. This means that /old-page would be like your user going to http://yoursite.com/old-page. Finally, https://yoursite.com/new-page is the full URL of where the destination of the redirect resides. Notice that this is a full URL, meaning you could create a temporary redirect to an entirely different website.

Redirecting entire sections

When redirecting an entire section of a website (https://yoursite.com/old-section) to a new part of your website (https://yoursite.com/new-section) you might think that you could follow the same pattern as the Redirecting one page to another example above.

Redirect 301 /old-section https://yoursite.com/new-section

Whilst this would mean any user visiting https://yoursite.com/old-section would be taken to https://yoursite.com/new-section, it unfortunately doesn't take into account any pages within /old-section such as https://yoursite.com/old-section/the-force-is-strong. Therefore, we need to use a RedirectMatch rule instead.

RedirectMatch 301 /old-section/(.*) https://yoursite.com/new-section

This will ensure that any pages within /old-section will also be changed to /new-section. For example: https://yoursite.com/old-section/the-force-is-strong will become https://yoursite.com/new-section/the-force-is-strong.

Please bare in mind that within your .htaccess file RedirectMatch takes priority over a regular Redirect rule, meaning that if you wanted one specific page within a section to go to someplace else then you'd need to order your rules appropriately. Just like in CSS, the lower down your redirect rule the higher priority it has, so make sure any individual Redirect rules are above your RedirectMatch ones.

RedirectMatch 301 /old-section/(.*) https://yoursite.com/new-section
Redirect 301 /old-section/a-specific-page https://yoursite.com/different-page-altogether

Further reading

Until next time! ✌


# Related posts

Like what you've seen? Want more? Check out similar posts on Assortment that you may find interesting:


# Comments

  1. #1 Posted:
  2. #2 Posted:

    NathanAmuts

    Hi there! globalpharmacycanada com great internet site http://canadiantrustpharmacyhd.com

    Reply to this comment
  3. #3 Posted:

    NathanAmuts

    Hello! drugs prescription very good website http://canadiantrustpharmacyhd.com

    Reply to this comment
  4. #4 Posted:

    Shawnjah

    Hello there! steroids online pharmacy very good internet site http://onlinepharmacycanadaus.com

    Reply to this comment
  5. #5 Posted:

    Shawnsmada

    Hi! online pharmacy viagra generic good website http://onlinepharmacycanadaus.com

    Reply to this comment
  6. #6 Posted:

    Shawnsmada

    Hi there! kaiser online pharmacy excellent website http://onlinepharmacycanadaus.com

    Reply to this comment
  7. #7 Posted:

    Jeacolive

    Levitra 10 Mg 2 Comprimidos Precio buy generic accutane online Buy Priligy Canada zoloft without a prescription Cialis 10mg Reviews Effets Secondaires Cialis Generique Buy Female Viagra Cheap Pak Rxlist buy viagra Viaga Lilly Cialis 10mg Et 20mg Hair Loss Buy Levothyroxine 88 Mcg tomar levitra Vente De Cialis Sur Le Net Diarex Pharmacy World viagra Cialis Fabrique En Inde Cialis Farmaco Generico

    Reply to this comment
  8. #8 Posted:

    MaxPowerempor

    Download not working dll from http://founddll.com/unarc-dll/ page. Fix the error now!

    Reply to this comment
  9. #9 Posted:

    Daviddow

    free cialis offer free cialis sample free cialis trial cialis free sample free cialis

    Reply to this comment
  10. #10 Posted:

    Frankpoups

    free samples of cialis cialis free voucher free cialis sample free cialis free cialis trial

    Reply to this comment
  11. #11 Posted:

    Frankpoups

    free cialis pills cialis com free offer free trial of cialis free 30 day trial of cialis cialis free shipping

    Reply to this comment
  12. #12 Posted:

    Frankpoups

    free cialis samples 30 day free trial cialis cialis free coupon cialis free offer free cialis offer

    Reply to this comment
  13. #13 Posted:
  14. #14 Posted:

    DannyPAx

    cialis free 30 day trial cialis free sample cialis coupons free cialis cialis com free trial

    Reply to this comment
  15. #15 Posted:

    SpencerAwata

    free trial cialis free samples of cialis by mail cialis coupon free trial cialis daily use cialis coupon 5mg

    Reply to this comment
  16. #16 Posted:

    Keithsub

    steroids online pharmacy latisse online pharmacy best canadian pharmacy online canadian online pharmacy tricare online pharmacy

    Reply to this comment
  17. #17 Posted:

    KevinNit

    costco pharmacy online legit online pharmacy costco online pharmacy online pharmacy hydrocodone ambien online pharmacy

    Reply to this comment
  18. #18 Posted:
  19. #19 Posted:

    AlvinMoign

    live pharmacy ce online good best online pharmacy canadian best online pharmacy viagra best online pharmacy adderall pharmacy online canada

    Reply to this comment
  20. #20 Posted:

    AlvinMoign

    best online pharmacy technician program pharmacy technician online training canadian pharmacies online pharmacy technician training online overseas best online pharmacy

    Reply to this comment
  21. #21 Posted:

    Jeacolive

    Cialis Dopage viagra cialis levitra combo packs Comprare Cialis Milano Acheter Cialis 20mg En Pharmacie Prix Cialis France Pharmacie Generic Soft Tab Viagra Where To Buy Clobetasol Dermatosis Secure Cialis In Turkei Kaufen Eastern Canada Pharmacy For Levitra Buy Propecia In California buy cialis online Buy Propranolol Canada No Prescription Propecia Ot Minoxidil

    Reply to this comment
  22. #22 Posted:
  23. #23 Posted:

    JarrodBiors

    viagra online canadian pharmacy reputable canadian online pharmacies pharmacy tech school online canadian online pharmacy american online pharmacy

    Reply to this comment
  24. #24 Posted:

    ThomasWah

    mexican online pharmacy pharmacy technician online courses what is the best online pharmacy without prescriptions needed ritalin online pharmacy online pharmacy no prescription needed

    Reply to this comment
  25. #25 Posted:

    ThomasWah

    accutane online pharmacy reputable online pharmacy kaiser online pharmacy cvs pharmacy application online best canadian pharmacy online

    Reply to this comment
  26. #26 Posted:

    ThomasWah

    legitimate canadian online pharmacies russian pharmacy online viagra online canada pharmacy online pharmacy viagra generic american online pharmacy

    Reply to this comment
  27. #27 Posted:
  28. #28 Posted:

    ftaletgshp

    viagra uk delivery easy get viagra prescription http://writeessayhtfd.com

    Reply to this comment
  29. #29 Posted:

    utaletoucz

    generic viagra reviews canada cheap viagra online china http://writeessayhtfd.com

    Reply to this comment
  30. #30 Posted:

    Showspogy

    In year 2019 there are lot of movies coming out. Obviously, there are Avengers part 2, new Star Wars Episode IX and many others. These are films that everyone wants to see, but sometimes you can miss it.It is where applications come in handy you can watch a movies for free. Btw, looking for the best app to watch free movies on your iOS phone? Then your should check Showbox app. This is the most famous app today that has a big library of tv shows and films. This app is also available for computer users. But your need to download it first to enjoy free movies Showbox for pc windows 10

    Reply to this comment
  31. #31 Posted:

    ptaletqekh

    viagra suomi farmasia apteekki kokemuksia buying viagra online safe http://lizlarssen.com

    Reply to this comment
  32. #32 Posted:
  33. #33 Posted:

    vtaletnlov

    order authentic viagra online is viagra illegal in the uk http://writeessayhtfd.com

    Reply to this comment
  34. #34 Posted:

    qtaletpwtf

    viagra for sale usa buy cheapest viagra online http://writeessayhtfd.com

    Reply to this comment
  35. #35 Posted:

    btaletwqeu

    cheapest viagra online in australia viagra prices costco http://writeessayhtfd.com

    Reply to this comment
  36. #36 Posted:

    EJsKa

    In year 2019 there are lot of films coming out. Obviously, there are Avengers part 2, new Star Wars Episode nine and many others. These are movies that everyone wants to see, but sometimes you can miss it.That's where apps come in handy you can watch a movies for free. Btw, looking for the best app to watch free movies on your iOS phone? Then your should check Showbox app. This is the most famous app today that has a big library of tv shows and movies. This app is also available for Windows users. But your need to download it first to enjoy free tv-shows download Showbox APK on PC

    Reply to this comment
  37. #37 Posted:
  38. #38 Posted:
  39. #39 Posted:
  40. #40 Posted:

    vtaletltsp

    extra super viagra 180 pills viagra canada http://lizlarssen.com

    Reply to this comment
  41. #41 Posted:

    famyrk

    where can i buy viagra http://canadlan-drugs.com

    Reply to this comment
  42. #42 Posted:

    atalettfkf

    buy viagra where buy real viagra online usa http://lizlarssen.com

    Reply to this comment
  43. #43 Posted:

    ytaletijmq

    how to buy viagra online safely viagra online in usa http://lizlarssen.com

    Reply to this comment
  44. #44 Posted:

    ptaletwwgi

    generic viagra price order herbal viagra http://writeessayhtfd.com

    Reply to this comment
  45. #45 Posted:
  46. #46 Posted:
  47. #47 Posted:

    KimerKa

    Interested in HARD ROCK? How about KISS? The band is on a tour this 2019 year all across Canada and USA. Visit KISS Concert 2019 Maryland Heights to know more about KISS concert dates in 2019.

    Reply to this comment
  48. #48 Posted:
  49. #49 Posted:
  50. #50 Posted:

    vtaletzdpl

    cheap female viagra buy viagra now http://lizlarssen.com

    Reply to this comment
  51. #51 Posted:
  52. #52 Posted:
  53. #53 Posted:

    CraigTer

    Hi there! order cialis buy tadalafil pills buy propecia excellent web page

    Reply to this comment
  54. #54 Posted:

    CraigTer

    Hello there! tadalafil where buy cialis order finasteride good internet site

    Reply to this comment
  55. #55 Posted:

    JamesMox

    Hello there! mexican online pharmacy beneficial website.

    Reply to this comment
  56. #56 Posted:

    JamesMox

    Howdy! walgreen online pharmacy excellent site.

    Reply to this comment
  57. #57 Posted:

    JamesMox

    Hi! online pharmacy percocet excellent website.

    Reply to this comment
  58. #58 Posted:

    JamesMox

    Howdy! indian online pharmacy excellent web page.

    Reply to this comment
  59. #59 Posted:

    JamesMox

    Howdy! azithromycin online pharmacy very good web page.

    Reply to this comment
  60. #60 Posted:

    JamesMox

    Hello there! ambien online pharmacy good internet site.

    Reply to this comment
  61. #61 Posted:

    JamesMox

    Hello there! canada pharmacy no prescription great internet site.

    Reply to this comment
  62. #62 Posted:
  63. #63 Posted:

    JamesMox

    Howdy! buy cialis medication beneficial web site.

    Reply to this comment
  64. #64 Posted:

    JamesMox

    Hello! buy cialis no prescription beneficial site.

    Reply to this comment
  65. #65 Posted:

    JamesMox

    Hello there! buy cialis no rx very good website.

    Reply to this comment
  66. #66 Posted:

    JamesMox

    Hi! buy cialis pills online beneficial website.

    Reply to this comment
  67. #67 Posted:

    Jeacolive

    Cephalexin For Toddlers Propecia Arranca Priligy Euro levitra commentaires Combivent Without A Prescription No Perscription Wellbutrin Viagra Necesita Receta Medica Comprare Viagra Senza Ricetta Will Keflex Cure Pneumonia viagra Prix Levitra Suisse Online Propecia Order Penisole Acheter Finasteride 5 Mg canadian pharmacy cialis Cephalexin Mouth Great Britain Domperidone Mexico Online Pharmacy Amoxicillin Shingles Kamagra Pharmacie France canadian cialis Buy Synthroid Cheap

    Reply to this comment
  68. #68 Posted:

    JamesMox

    Hi there! purchase cialis online no prescription very good internet site.

    Reply to this comment
  69. #69 Posted:

    JamesMox

    Howdy! buy cialis no rx very good web site.

    Reply to this comment
  70. #70 Posted:

    Danielchori

    Hi! buy tadalafil good website.

    Reply to this comment
  71. #71 Posted:

    Danielchori

    Hello! where buy cialis great internet site.

    Reply to this comment

# Leave a comment

No wookies will get this, its just for your Gravatar image.

Basic markdown supported, go to FAQ for more info.