07/28/22 Franck Bugnet Guide

How to avoid PrestaShop 404 errors by implementing 301 or 302 redirects

Following the migration of my site to a new server, and the upgrade of PrestaShop from 1.4 to 1.6, I had to understand the mysteries of the redirection techniques to maintain its SEO.

Why is it important to know how to write 301 redirects?

A 404 error, as reminder, indicates a URL pointing nowhere, a page not found. Google does not like in general, but your visitors either and they are likely to look elsewhere.

SEO is a long, long job, for each page. If the URL of a page change a bit, even one single letter, there will be at best a 404 error, and if nothing is done then all the referencing of this page will be lost.

Further more, links to this page from other sites, will no longer work.

Thus, when major modifcations of a site are made, such as a change of the CMS version, any indexing, ranking and knowledge of the site by the search engines can be permanently lost. This is sufficient to kill a company.

Therefore, priority must be given to the conservation of URLs for all pages. But sometimes it is not possible or too complicated to implement. In this case, the best solution is to make definitive redirects, called 301.

A 301 redirect is redirecting the old URLs permanently to the new URL of the page. This goes for Google but also for inbound links.

Here is the curve of errors on this site URLs for Google every morning at the time of migration and version change. Within days, it's climbed to 150 mistakes I quickly corrected largely through 301 redirects:

Number of URL errors seen by Google

Where to insert redirections of URLs ?

There are several places, such as in the header of the page itself, using PHP language:

header ("Status: 301 Moved Permanently", false, 301);

header ("Location: http: //ma_nouvelle_addresse.html");exit ();?>

Very tedious if you need to redirect a large number of pages. And not at all suitable for PrestaShop because the header is common to most pages.

The simplest and most common method (and therefore the one that I propose) is in the famous .htaccess.

A .htaccess file has to be placed in the root of PrestaShop. If you have activated simplified URLs functionality (more than recommended) in PrestaShop, this file has already been created for you. Any text editor is eall you need to edit it.
The file created by PrestaShop contains a part in which it is strongly forbidden to add a single line, as your additions will be lost when PrestaShop must regenerate this file. Parts outside this area will be kept.

This area begins with # ~~ ~~ start and end at the # ~~ ~~ end. So you can add your redirection lines before or after party. I suggest you add them before.

Lines starting with a # are comments, ignore when operating the .htaccess.

How to write a line redirection?

There are two methods.

Method 1, using the Redirect command:

The following rather simple syntax can be used:

Redirect 301 ancienneURL nouvelleURL

Example: Redirect 301 /forum.php http://www.sitolog.com/fr/forum

You have to respect some rules:

The first URL must begin with / .It should therefore not contain the site's domain name.

The second URL instead must contain the domain name and the prefix http: //

301 indicates that the redirect is permanent. it is also possible to make such temporary redirects with code 302.

Note: with a similar syntax, using "gone" instead of 301, it is also possible to indicate to search engines that the page no longer exists (caution it will deindexed!).

Ex: Redirect gone http://www.sitolog.com/fr/my_old_deleted_page.html

This method 1 / suffers a big limitation: it is not possible with this syntax to manage the URLs parameters. You know these common ?id_lang=2 or ?content_only=1 sometimes located at the end of URLs.

It is therefore not possible to manage multi languages ​​with such redirection syntax.

Method 2, using the RewriteRule command:

Say it right away, it is more complicated to learn and to master. But the effort is worth it because it is very powerful and allows to get you out of complex situations.

At first it looks like the Chinese. Like this two lines example, using RewriteCond command to condition the redirection:

RewriteCond %{REQUEST_URI} ^ / forum. *

RewriteRule ^forum-([0-9] +)([az]).html http://www.sitolog.com/fr/forum/$1$2 [R=301,L]

There are many tutorials on the net explaining this syntax in detail, especially the command "RewriteRule". So I'm not going to throw myself again in one more exercise in style, but I chose for you those which have been most useful to me:





The above example also uses regular expressions, such as ([0-9] +) ([az]) which means for example "a sequence of digits followed by a lowercase letter".

This allows in a single line to redirect an almost infinite number of pages. For dynamic sites created with PrestaShop, which consist of a large number of pages sharing mostly the same URLs, this is the ultimate tool.

Honestly, every time I have to write, or even just understand a regular expression, I do not hesitate longer and rapidly re-open one of the many documentions available on the net, REGULAR EXPRESSION TUTORIAL.

It's not like riding a bike, it is easily forgotten and it does not return quickly :-(

All this gives us roughly for the example above:

RewriteCond indicates a condition, only the pages respecting the conditions will be redirected

%{REQUEST_URI} indicates that the condition refers to the part of the URL before the URL parameters. You can used instead %{QUERY_STRING} to set a condition on the parameters such as ?id_shop=1

^/forum.* indicates the URL should contain /forum

RewriteRule is redirection itself.

^forum.-([0-9]+)([az]).html defined what the old URLs redirect format has to be, using a regular expression to point a set of pages in a single line.

http://www.sitolog.com/fr/forum/$1$2 defined the new URLs.

$1 and $2 are variables to retrieve the values ​​of regular expression old URLs for reuse in the news. Such as the 7s article of the forum. $1 refers to ([0-9]+) and $2 refers to ([az]).

[R = 301, L] : 301 defines the type of redirection, and stop it (it is possible to chain redirects).

With this redirection, for example, we will all pages like http://www.sitolog.com/forum-7s.html kind redirected to http://www.sitolog.com/fr/forum/7s format. Powerful isn't ?

Note that to work, redirection rules require that the server redirection feature is enabled. This is why you need to insert, upstream of all these rules, these few lines:


RewriteEngine on

And of course make sure that the PHP extension mod_rewrite.c is installed and enabled on the server.

Is it risky? YES !

Establishing permanent redirects in the .htaccess without being certain not to be wrong can be catastrophic.

Indeed, if you redirect your pages towards Inexistentes URLs, you'll make things worse.

It is even possible to write rules that buckle on themselves and are going to crash your server (well almost, they are still protected against such errors).

Also be aware that any syntax error in the .htaccess systematically generates a blank page with a mysterious 500 error.

So please implement slowly and carefully.

My best advice is to always test a redirection rules before inserting it in the .htaccess.

For this there is a simple free and magical tool, HTACCESS TESTER.

It's very simple to you :  you enter your forwarding rule, your starting URL and it gives you the rewritten URL. All you still have to do it to check that it takes you well on the right page.

Example, validation of the above rewrite rule:

Validte your rewritte rules with htaccess tester .htAccess

How to monitor 404 errors?

Go to "Google Webmaster Tools", recently renamed SEARCH CONSOLE, then in the "Exploration" menu, then to "Crawl errors."

You will find regularly updated, the list of 404 errors seen by its search engine. You can even check in real time that your redirects are working and if that's the case, tell Google that the error is corrected.

Last tip for the road: a 301 must be maintained throughout the lifetime of the site. Never say, "I do not know what is this old rule, I delete it...".


Category's other articles Guides and tips on PrestaShop and online selling

This article was useful ? Share it !