- Your cart is currently empty.
How to Set Up 301 Redirects in .htaccess
A 301 redirect is a permanent redirect that redirects visitors and search engines from one URL to another. There are also temporary 302 redirects, but these are rarely used in practice, as in most cases the change of web address is done for a longer period of time or permanently.
Permanent redirects are used in a variety of situations:
- when we move a website from an old domain to a new one,
- when we want our website address to start with or without “www”,
- when we install an SSL certificate on a website,
- when we change the URL link of a sub-page,
- when we want to get rid of an extension at the end of links (.html, .php, …),
- when we change the structure of a web page.
It is also a good idea to redirect when a wrong link to one of the subpages on our site is published on a foreign website. For example, if someone posted a link to our blog post and got “stuck” when writing the URL address, it would make sense to redirect the wrong address to the right one.
Why are 301 redirects so important? First of all, because they redirect visitors to relevant web addresses, and they are also closely linked to web optimisation. Permanent redirects transfer power from one domain or sub-page to another, which means that when the URL changes, we don’t lose visibility in search engines (e.g. Google). According to experts, a 301 redirect is expected to retain 85-90% of its power.
How to edit the .htaccess file?
To make sure we don’t just stick to boring theory, here are some examples of the most common 301 redirects. These are written to the .htaccess file, which is located in the Root Directory of the domain or subdomain. Since this is a hidden file, make sure that you have the display of hidden files enabled in your cPanel control panel or FTP client. If we find that we don’t have an .htaccess file, we need to create one first.
In the File Manager of the cPanel control panel, we enable the display of hidden files by clicking on the “Settings” button in the top right corner and checking the “Show Hidden Files (dotfiles)” option. A new file is created by clicking on the “+ File” icon located in the top left corner.
To edit the .htaccess file, right-click on it and select “Edit“. In a new tab, we will see a code editor where we can enter commands for permanent redirects.
Common examples of 301 redirects
Writing the commands for the basic redirects that we will present in today’s article is quite easy. However, we should mention here that for more complex redirects, we use so-called Regular Expressions (Regex for short) to redirect large groups of web addresses according to written rules.
In fact, we’ll use some of the simplest Regex expressions today, but we’ll write more about them another time. So let’s focus on the most common examples of permanent 301 redirects.
Redirecting a domain without www to a domain with www
Since search engines may treat nasadomena.si and www.nasadomena.si as two different websites, it is recommended from a web optimisation point of view that the website is only accessible from one address.
When setting up a new website, it does not matter which option is chosen. The decision depends entirely on personal preference. However, if your website has been online for a long time, it is advisable to check which web address has more links from other websites. If there are more links pointing to the www version, the 301 redirect is done by entering the following in the .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^nasadomena.si [NC]
RewriteRule ^(.*)$ http://www.nasadomena.si/$1 [L,R=301,NC]
Redirect a domain with www to a domain without www
We use a permanent 301 redirect from the web address www.nasadomena.si to nasadomena.si when we prefer an address without the www start record. It also makes sense to use a redirect when most of the links are directed to a web address without the www subdomain.
In this case, we need to enter the following code in the .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.nasadomena.si [NC]
RewriteRule ^(.*)$ http://nasadomena.si/$1 [L,R=301,NC]
Redirect domain from http to https
When an SSL certificate is installed on a website, it is no longer accessible only on http connections, but also on https. https is a secure connection where the data transmission is encrypted and thus protected from potential online attackers.
Since we don’t want our site to be accessible even on an http connection, we need to make sure that we redirect it appropriately. This is also necessary in order to maintain our rankings in search engines, especially Google. Otherwise, traffic to the website may drop significantly.
To permanently redirect a web page from http to https, use the following notation:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NO]
As Google is very committed to the security of the web, at the end of 2014 they decided to treat SSL certificates as a positive signal when ranking search results. In addition, Google experts Gary Illyes and John Muller have made it publicly known that the full strength of the links is preserved in permanent redirects from http to https.
Redirecting an old domain to a new one
Although domain switching is generally discouraged, sometimes there is simply no other choice. We may have had to change the name of our company, which makes the old domain no longer an option. We may have decided to “rebrand”, which means that we also need a new domain name. It is also possible that we simply got tired of the old domain name.
Whatever the reason for switching, it would be a shame if all the effort we put into building the visibility and strength of the old domain name went to waste. It will be much better to redirect the old domain to the new one, as this is good both from the point of view of visitors and web optimisation.
To permanently redirect an old domain to a new one, we write the following in the .htaccess of the old domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^staradomena.si [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staradomena.si [NC]
RewriteRule ^(.*)$ http://novadomena.si/$1 [L,R=301,NC]
Redirect a single subpage
We may have a posting on a web page which, over time, it turns out that it was worthwhile to change its URL. An example of this would be when we have a year-specific text on the page (e.g. “Number of flu cases 2016“). If we plan to update the article every year, it is best to avoid the year in the URL from the start, but if we have forgotten this, we are left with another solution. Change the web address and create a permanent redirect in .htaccess.
Again, the permanent 301 redirect has two effects. Firstly, it has a positive impact on the user experience, as visitors to the website see the relevant record in the URL, and it transfers most of the power from the old page to the new one. This means that the redirect retains the positions achieved in the search engine.
1. If you want to redirect within the same domain, write the following in the .htaccess file:
Redirect 301 /old-site /new-site
2. If we want to redirect a sub-page to a sub-page of a different domain, we use the following:
Redirect 301 /old-site http://novadomena.si/nova-podstran
The above record is entered in the .htaccess of the first domain, i.e. the one where the subpage with the web address …/old-subpage existed.
Redirect to remove the URL extension
As a final example of the simpler permanent redirects, let’s look at how to achieve the removal of the file extension from the URL. The main reason to do this is that shorter web addresses look nicer and are easier for visitors to remember. In addition, if we ever decide to change the programming language (e.g. from HTML to PHP), the URL structure of our website will not be affected.
If the links of our website end with .htm, .html, .php, .aspx or any other extension, we can exclude the extension from all URLs with just a few lines of the “Regex” expression. This is done by writing the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [QSA]
The above code is used if the individual subpages of our website are files with the extension .php. If our website is static and the .html extension is visible in the URLs, we adjust the code accordingly – in two places we replace .php with .html.
Before you start editing .htaccess, make a backup copy of the .htaccess file. We also advise you to thoroughly check that redirects are working correctly after each change. If you are worried that the redirects will “break” your website, it is definitely best to seek professional help.







COMMENT THE POST
Your comment has been successfully submitted
The comment will be visible on the page when our moderators approve it.