Redirections in .htaccess for PrestaShop, how do I do them?

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

Yesterday I had to go to the Hospital, which is on the other side of the city, so it takes me 45 minutes to get there or more, depending on the traffic. As I have no idea, I put the GPS to go as co-pilot controlling the route and I noticed that every few kilometers the App had to “recalculate” the route and if the application is updated periodically as expected, the suggested routes should be the good ones…

Let’s talk about Redirections in .htaccess for PrestaShop that help us manage 404 errors, correct domain changes and redirect old urls to new urls.

What happens if you had a Store in PrestaShop with the domain comerdeperros.es and you started to call it dcdistribuciones.es for commercial reasons? Well, if you did things right and applied the 301 redirects correctly, now your store visitors have to be able to reach the products that you already had indexed in Google (and other search engines) and thus avoid the dreaded 404 errors by using them effectively.

Redirections, so important and so ignored on many occasions by users who change domains, CMS, who delete categories or merge them, who learn SEO on-page in fits and starts and detect the “bugs” that they have been leaving for the road. It’s time to get down to business and get the redirects right!Google, Bing, your buyers and your positioning will appreciate it shall we get to it?

If you are still not clear about what a redirection is, where do I explain it so that you can refresh it in your memory and you can better understand what this new article is about.

What does a default PrestaShop .htaccess file contain?

When you install PrestaShop 1.6.x in a Hosting, a file is generated .htaccess which by default contains certain information for the website to work correctly.

# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again # .htaccess automatically generated by PrestaShop e-commerce open-source solution # http://www.prestashop .com – http://www.prestashop.com/forums

  SetEnv HTTP_MOD_REWRITE On  RewriteEngine on #Domain: domain.com RewriteRule .  - RewriteRule ^api$ api/ RewriteRule ^api/(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 # Images RewriteRule ^()(-*)?(-+)?/.+ .jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg RewriteRule ^()()(-*)?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/ p/$1/$2/$1$2$3$4.jpg RewriteRule ^()()()(-*)?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/ $3/$1$2$3$4$5.jpg RewriteRule ^()()()()(-*)?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3 /$4/$1$2$3$4$5$6.jpg RewriteRule ^()()()()()(-*)?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/p/$1 /$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg RewriteRule ^()()()()()()(-*)?(-+)?/.+.jpg$ %{ ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg RewriteRule ^()()()()()()()(-*) ?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg RewriteRule ^( )()()()()()()()(-*)?(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5 /$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg RewriteRule ^c/(+)(-*)(-+)?/.+.jpg$ %{ENV:REWRITEBASE}img/c /$1$2$3.jpg RewriteRule ^c/(+)(-+)?/.+.jpg$ %{ENV:REWRITEBAS  E}img/c/$1$2.jpg # AlphaImageLoader for IE and fancybox RewriteRule ^images_ie/?(+).(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 # Dispatcher RewriteCond %{REQUEST_FILENAME} -s RewriteCond %{REQUEST_FILENAME} -l RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php  AddType application/vnd. ms-fontobject .eot AddType font/ttf .ttf AddType font/otf .otf AddType application/x-font-woff .woff   Header add Access-Control-Allow-Origin "*"   #If rewrite mod isn't enabled ErrorDocument 404 /index.php?controller=404 # ~~end~~ Do not remove this comment, Prestashop will automatically keep the code outside this comment when .htaccess will be generated again

From this point on and depending on your installation, the file may contain more or fewer instructions.

If you have friendly URLs enabled in preferences, SEO and URLsin section Link configuration and the .htaccess file does not exist, you will come across a Error 404 when trying to load the web.

Not Found
The requested URL /en/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

This usually happens when a Hosting website is transferred and the view of hidden files has not been previously enabled, which causes us to leave the .htaccess file along the way in the process of transferring the web, either from a local installation (your computer) or from a different server than the current one.

If you want to know how to avoid leaving hidden files behind because you don't see them, check the blog article

Some redirects in PrestaShop using .htaccess

One of the first redirects you should have included in your file .htaccess is the redirection of the domain without aliases www to the domain with alias “www” or vice versa, depending on whether you want to see the web with or without alias www.

RewriteEngine on RewriteCond %{HTTP_HOST} ^yourdomain.com RewriteRule ^(.*)$ http://www.yourdomain.com/$1

Note By default PrestaShop (nor other CMS) perform this type of redirects automatically unless indicated.

Where to put the redirects in .htaccess for PrestaShop?

The .htaccess file is a dynamic file that is generated and regenerated as we make changes to preferences, SEO and URLsso changes applied in this section can modify the content of .htaccess without you realizing it unless you monitor or review said file.

Important The recommendation is always to make a backup copy of said file before regenerating urls.

By default, PrestaShop adds the following in the first line of the .htaccess file:

# ~~start~~ Do not remove this comment, Prestashop will automatically keep the code outside this comment when .htaccess will be generated again

And below put everything related to mod_rewriteDomain, Images, Dispatcher, mod_expires, mod_headers and other code, closing the file with the following line:

# ~~end~~ Do not remove this comment, Prestashop will automatically keep the code outside this comment when .htaccess will be generated again

The redirects in this file should ideally be added before the first quoted line:

My store is in another folder...

Something quite common is that a PrestaShop installation is not necessarily in the main Hosting folder, which in WePanel-based Hosting panels is public_html.

If you have several websites on your Hosting, whether they are all PrestaShop installations or a mix of several content managers (WordPress, Joomla, etc.), it is quite likely that you have the installations in separate folders, including the installation of the main domain.

In cases like this, the appropriate thing is that the redirections are in the .htaccess file of the main folder of the Hosting, in this case / public_html where the first thing that must appear are the codes necessary to redirect the main domain to a folder and below all the redirections 301 that for different reasons you have to apply so that your website works correctly and you do not lose visits due to 404 errors, etc.

In this case the .htaccess file in /public_html will contain code similar to the following:

This example illustrates a classic store that is not hosted in the main folder of the Hosting but in a subfolder and that also has to carry out numerous 301 redirects to correct old urls to new urls.

As the web in this case is in a subfolder, in this example it is in /public_html/cilene2 you will notice that between lines 1 and 8 the main domain is redirected to the folder where the store is installed and from line 11 301 redirects are applied to correct problems of links not reached detected by Google Search Console.

The .htaccess file of the folder where the installation is, for this example it is located in /public_html/cilene2/ will contain those codes that PrestaShop establishes by default when said file is installed or regenerated, plus additions that have been made later to manage, for example, image cache expiration times and others, but no 301 redirect will appear since they were declared in the .htaccess file from the main folder of the Hosting.

Redirect old url to new url

It is the most common case that can be found in PrestaShop when you have changed the domain website, you have moved the installation from a subfolder to the main folder and before you had an additional path in the domain to access the website, for example:

http://foodfordogs.es/tienda

And now it has been renamed:

http://www.dcdistribuciones.es

For these cases it is very important that you first analyze Google Search Consolein section crawl errors if there are 404 errors derived from the change of urls.

If it appears as I illustrate in the capture, perfect! there is nothing to worry about. If, on the other hand, you have errors, you will have to review and write down all the errors to convert them into redirects in case they are errors related to urls not reached because there were changes in the URIs.

For these cases, it is important that you begin to draw up an action plan to reduce as soon as possible the negative impact that these errors can cause on your positioning in case you are a little lazy in these matters and let it go. I guarantee you that by itself it will not be fixed no matter how much you look at it if you do not act.

Start slowly, try first with the first erroneous url that Google sings, write it down, copy it and try to open it in another tab of your browser, if it returns a 404 you know, it does not exist, you deleted it or now it is called something else.

Open your .htaccess file (before make a backup of it for security) and in the first line make a space (Enter) and write more or less with this syntax (adapting the old and new url to your case) add the following:

Redirect 301 /old-page http://www.mitienda.com/nueva-pagina

Save the changes in the .htaccess file and try again to load the “old” and wrong url currently affected by a 404 error

If you did it right you should be landing on the new url transparently, which would solve the problem. Phew, what a relief right!

That you have 156 urls with 404 errors? patience and good food as my grandmother would say, and start adding each of the "old" (wrong) urls with the aforementioned syntax and redirecting them to the "new" url to reduce errors.

That an "old" url now no longer leads to content because you don't use it? Nothing happens either, listen to me, it is better to take the orphaned urls to the main url of the Store, than not to take them anywhere. A solution...

See also  The weather script does not load on your AEMET website
Loading Facebook Comments ...
Loading Disqus Comments ...