Permanent 301 Redirects via .htaccess: 11 Examples to Copy

redirection 301

Do you want to do 301 redirects via the .htaccess file and need a little help and concrete examples to get by? Here is a list of more than 10 concrete examples with the code to add in the .htaccess in order to configure effective and very useful 301 redirects for the referencing of your website.

Back to the basics: what is a 301 redirect and what is it for?

The redirects 301 (or permanent redirects), are, unlike temporary 302 redirects, redirects that serve to indicate to Google that a page has been deleted or moved permanently and that a new nearby page is available.

This type of redirection is very useful in SEO since it allows to transmit as much as possible the history, the positioning and the links of the old page to the new one. to which the 301 redirect is made.

How to do a “simple” 301 redirect (from page to page) via the .htaccess file?

To make a simple 301 redirection from the .htaccess file (file located at the root of your website hosting and which can be retrieved and modified via FTP access), you just need to follow this frame by replacing the starting location and the ending location:

RedirectPermanent /webmarketing/seo/seo-google-confirme-mise-a-jour-algorithmique-1er-aout-2018-13953/

The directive RedirectPermanent in the .htaccess file, which generates a permanent 301 redirect, can be placed anywhere in the .htaccess file.

In our example, we indicate that we want to redirect the page /webmarketing/seo/seo-google-confirme-mise-a-jour-algorithmique-1er-aout-2018-13953/ to this url: because the ID of the article has changed but we do not want to lose the history of the latter nor its possible SEO positioning.

This same redirection can also be performed using the following code which will do exactly the same thing:

Redirect 301 /webmarketing/seo/seo-google-confirme-mise-a-jour-algorithmique-1er-aout-2018-13953/

Caveats when changing the .htaccess to set up 301 redirects:

  • Modifying the .htaccess file of your website is not trivial, improper handling can make your site inaccessible. If you are not sure of yourself and as a precaution: before any modification, we strongly recommend that you make a backup of your original .htaccess file to be able to re-import it via FTP in case of problem.
  • If you have many 301 redirects to perform and you have access to your hosting’s VHOST, it is strongly recommended that you configure 301 redirects through this to avoid any impact on your website loading time. The larger the .htaccess file, the more your website can suffer.

10 examples of useful redirect rules to copy and paste

1- How to redirect all the pages of a site to their HTTPS version in 301?

This code to add in the .htaccess file will automatically configure the 301 redirects from the version without HTTPS to the version with and from the version without www to the version with HTTPS. All HTTP pages will be redirected in 301 to their equivalent in HTTPS.

Redirection of the HTTP version to HTTPS:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !
RewriteRule ^(.*)$$1 [R=301,L]

Force the www to HTTPS:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteCond %{REQUEST_URI} !
RewriteRule ^(.*)$$1 [L,R=301,NC]

Of course, for this code to work on your site, you will need to replace all occurrences of LEPTIDIGITAL and also be sure to replace the domain name extension with your own.

2- How to redirect only certain pages in 301 to HTTPS?

This code to be placed in your htaccess file allows you to do a selective 301 redirect of pages and folders to HTTPS, if you do not want to switch everything at once:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/exempledepage.html$ [OR]
RewriteCond %{REQUEST_URI} ^/dossier/page-2.php$ [OR]
RewriteCond %{REQUEST_URI} ^/dossier-2/(.*)$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3- How to redirect an entire domain to a single page?

This code will redirect all of the domain’s pages and files to the LEPTIDIGITAL home page:

Redirect 301 /

4- How to redirect in 301 your domain name without www to the version with www?

RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^ [nc]
RewriteRule ^(.*)$$1 [r=301,nc]

5- How to redirect in 301 your domain name with www to the version without the www?

This redirection rule will do the opposite of the previous one.
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule .?{REQUEST_URI} [R=301,L]

6- Redirect old directories to new ones in 301

RewriteRule ^/?ancien_repertoire/([a-z/.]+)$ nouveau_repertoire/$1 [R=301,L]

7- How to redirect all pages to 301 by changing the file extension (HTML to PHP for example)?

This directive will redirect in 301 all pages with the .html extension to their equivalent with the .php extension:

RedirectMatch 301 (.*).html$$1.php

8- Redirect all .php and .html extensions to the URL without

This rule is valid if you have urls that end in both .php or .html in some cases and you don’t want to display an extension at the end of your URLs:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([a-zA-Z0-9]+)$ $1.html [L]

9- Force the trailing slash at the end of URLs to avoid having two identical URLs with the same content

This rewrite rule is very useful for forcing all URLs on the site without a “/” at the end to have one. This avoids duplicate content and is therefore better for your SEO.
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

10- How to redirect URLs with Query Strings in 301 to URLs without?

This code will redirect in 301 all the pages that contain? Categorie = in the URL to their equivalent without query string:

RewriteEngine On
RewriteRule ^/?categorie/([^/d]+)/?$ index.php?categorie=$1 [L,QSA]


Original URL before the rule was activated:

Destination URL once the rule is active:

9- How to redirect in 301 a blog in subdomain to a new domain with the blog in directory?

This .htaccess directive should be placed in the directory containing the blog in subdomain:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*){REQUEST_URI} [R=301,NC]
RewriteRule ^(.*){REQUEST_URI} [R=301,NC]

This code to be adapted will act as follows:

  • All the pages accessible on a type url will be redirected in 301 to their equivalent on the new domain and directory:

How to test your 301 redirect rules via htaccess before passing them online?

To limit the risk of your website crashing (the .htaccess file is very sensitive), we strongly recommend that you test your 301 htaccess redirect rules with this very simple and effective online tool :

Overview of Htaccess Tester, a free online tool to test your 301 redirects configured via htaccess

To use this tool and test your redirect rules before they are active in production, you just need to:

  • copy – paste the code you have adapted in the section ” Paste your .htaccess rules into the form “(Which literally means” Paste your .htaccess rules into the form“),
  • test the URL of your choice which should be impacted by your 301 redirect htaccess rule.

That’s it, so you can make sure before modifying your .htaccess file that your rules will work exactly as you want them to.

(Yes, the tool is not French or English, but Belgian, like what, the Belgians also have good developers / referencers in their ranks 😉… (joke ofc).

Do you need another code to properly redirect all your pages in 301 via the .htaccess file? Do not hesitate to leave a little comment, we will answer you with pleasure!

Do you want to receive our best articles?

By subscribing to the LEPTIDIGITAL newsletter, you confirm that you have read our confidentiality and data processing policy (your email will never be shared and will remain anonymous 😉).

See also  Definition, Tools and Examples of Use

Leave a Comment

Your email address will not be published.