What is the .htaccess file?
Abbreviation of “hypertext access”, the .htaccess file is located at the root of a web server. It is used to configure certain features of a website hosted on an Apache server. In general, this file allows you to edit rules of conduct to control the behaviour of the server for a website.
More concretely, it allows in particular the following functionalities:
- Protect directories and folders of a site with a password,
- Prohibit or authorize user browsing based on their IP address,
- Restrict access to one or more page(s) of the site,
- Define a file name that will be displayed when an address points to a directory,
- Customize an error page that will be displayed when a 404 error is generated,
- Set up URL redirects (what interests us here).
Specifically, in this article, we’ll see how best to use the .htaccess file to perform redirects on your website. But first, let’s review the basics.
What is a 301 redirect?
The 301 redirect is a permanent URL redirect. It is a directive to permanently move a web page to a different URL. When a 301 redirect directive is given for URL A to URL B, internet browsers will automatically load URL B.
In this case, the 301 redirect is the recommended guideline when you want to permanently move a page.
Since the redirect is permanent, the directive also helps tell search engines to update their index to point to the new address.
From a technical SEO Service point of view, this means that the permanent redirection from URL A to URL B also makes it possible to repatriate the positioning of URL A to this new URL B.
It is therefore essential to carefully plan your redirection strategy in the context of migration, for example. A well-crafted redirect plan and rules ensure that your website traffic is not lost while avoiding errors and redirect loops. Be sure to test all redirects before going live to ensure they work properly and lead to valid pages.
What is the difference between a 301 redirect and a 302 redirect?
Unlike the 301 redirect, the 302 redirect is a directive telling browsers and search engines that the page has moved temporarily.
When the search engine detects the HTTP status code 302 for a URL, it understands that the page is moved temporarily, leaving, in theory, the initial page in its index. In this way, unlike the 301 redirect, the SEO authority of the initial URL is not transferred to the destination URL.
There are multiple reasons to use the .htaccess file to perform redirects on your site. We will now list several possible scenarios while illustrating them with examples of possible redirection rules.
How to redirect a single URL via the .htaccess file?
In general, the .htaccess file is especially useful for configuring massive redirection rules (redirection of URLs in the plural), in particular using regex.
However, it is possible to simply redirect one URL to another. Here is the rule to use in this case:
Redirect 301 /ancienne-page/ /nouvelle-page/
/old-page/ being here the URI of the page to redirect and /new-page/ being the destination page. The code “301” present in this example is the code of redirection carried out (permanent redirection) as we mentioned above.
Therefore, to perform a 301 redirect, it is also possible to use the RedirectPermanent directive which works in the same way:
RedirectPermanent /ancienne-page/ /nouvelle-page/
How to redirect http to https with .htaccess file?
The rule below is used to redirect all URLs from http to https. All the URLs of your site will be redirected here in 301 to the equivalent address in https.
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How to redirect www to non-www?
In case you do not want to use the www version for your URL format, it is possible to use the .htaccess file to redirect all your URLs in www format to the non-www version permanently.
Here is the 301 redirect rule to apply:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]</IfModule>
How to redirect a directory?
Here is the procedure to follow to redirect a directory to a new destination folder:
RewriteRule ^/?dossier-1/(.\*)$ /dossier-2/$1 [R,L]
How to redirect a domain name?
The rule for redirecting an entire domain to a new domain name is as follows:
Redirect 301 / http://www.domain.com/
It is also possible to refine this rule to redirect the entire domain but, this time, to a directory. An example:
Redirect 301 / http://www.domain.com/repertoire/
Conclusion
In summary, the .htaccess file is a very handy tool for setting up bulk redirects on your website. We have seen that by using regular expression patterns, this file comes in handy for effectively redirecting all of your site’s URLs to new addresses.
Remember that the redirect rules and examples mentioned in this article are not exhaustive. Other scenarios are possible and these rules can be readjusted depending on the case. In any case, it is important to carefully plan your redirect strategy to avoid errors and redirect loops.
It is also important to note that including redirect rules in your site’s .htaccess file can be dangerous because this file is very sensitive. Indeed, a bad manipulation can generate many problems (in particular the generation of massive 404 errors if redirections are badly carried out), or even make your site inaccessible.
An SEO agency in Lille can support you in your migration or redesign project by carrying out the appropriate redirection plan or, quite simply, to support and advise you on your redesign or migration project by recommending the best practices on -measure against your site.