Magento – How to add 301 redirection rule magento2

.htaccessmagento-2.1.8

How to add 301 redirections for the base URL in Magento 2.1 via .htaccess.

http://www.example.com/

http://www.example.com

http://example.com

http://example.com/

https://example.com

http://example.com

https://www.example.com/

I want to all these url versions 301 redirected to https://www.example.com

Best Answer

Here it is step by step answer for it.

  • Log into your Magento 2 Store Admin panel
  • Click on ‘Marketing’ module on the left toolbar
  • Under SEO & Search -> click on URL Rewrites
  • Click the Add URL Rewrite button located on the right top corner
  • From the ‘Create URL Rewrite’ drop-down menu, select Custom as the option
  • Next, select Default Store View from the Store drop-down list
  • Enter the old URL (which needs to be redirected to the new URL) in the request Path field
  • Enter the target URL (the new destination URL) in the Target Path field
  • Choose the redirect type as Temporary (302) or Permanent (301) from the Redirect Type drop down menu
  • Click the Save button on the top to save the changes made. A confirming messaging indicating the URL Rewrite has been saved will pop up.

Or if you want this via .htaccess then please check below.

Rather than redirecting an entire domain, this 301 Redirect – simply named “Redirect 301” is a single-line, single-page redirect.

For example:

# Redirect [CODE] [OLD PATH] [NEW PATH]
Redirect 301 /my-page.html /my-new-page.html

This would redirect the URL:

<a class="vglnk" href="http://www.example.com/my-page.html" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>mydomain</span><span>.</span><span>com</span><span>/</span><span>my</span><span>-</span><span>page</span><span>.</span><span>html</span></a>

To:

<a class="vglnk" href="http://www.example.com/my-new-page.html" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>mydomain</span><span>.</span><span>com</span><span>/</span><span>my</span><span>-</span><span>new</span><span>-</span><span>page</span><span>.</span><span>html</span></a>

If you wish to 301 redirect to another domain name you can simply add the domain to the destination URL:

Redirect 301 /my-page.html <a class="vglnk" href="http://www.my-new-domain.com/my-new-page.html" rel="nofollow"><span>http</span><span>://</span><span>www</span><span>.</span><span>my</span><span>-</span><span>new</span><span>-</span><span>domain</span><span>.</span><span>com</span><span>/</span><span>my</span><span>-</span><span>new</span><span>-</span><span>page</span><span>.</span><span>html</span></a>

Note: The 301 part of the above code can be changed to represent any http status code you wish to use e.g. 301, 503 etc.

Hope it will help you.

Thanks