How to add custom URL redirects for your Magento site hosted with MDOQ.
A common requirement for any website is the ability to be able to add custom redirects for legacy URLs. To do this within MDOQ is very simple.
N.B if you're looking to configure a domain redirect, for example example.com => www.example.com please see Redirect root domain to www
In this guide we are going to assume that the media path has changed from /pub/media/myimage.jpg to /media/myimage.jpg this means we need to redirect all request from /pub/media/* to /media/* . Though you can add as many as you like, there are no restrictions.
- Create a development instance
- Once the instance has rolled up create the file: mdoq/nginx/templates/default_https.conf with the content:
location ~ ^/pub/media/(.*)$ {
return 301 $scheme://$http_host/media/$1;
}
There are a few things going on here:
- The templates directory (mdoq/nginx/templates) is where we place files that we want MDOQ to use as part of the Nginx component creation.
- The file default_https.conf is a file that will be evaluated for all HTTPS requests before they are passed on to Magento.
(If you have multiple domains and you want this just to be for a specific domain you would use the file name www.customdomain.com_https.conf ) - we are using the $http_host variable instead of hard coding the domain, so that it works without modification for all domains. (This enables us to test the file on a MDOQ instance and get the correct redirect domain)
- Then within MDOQ select your instance, select "Sync" > "Show More" and check the "Nginx" component. Then click proceed.
- Once the synchronization has completed you will be able to test the redirect. You could run something like:
curl -IX GET https://mysite-111111.03.mdoq.io/pub/media/foo.jpg
Which should return something like:HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.3
Date: Fri, 17 Dec 2021 14:51:58 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://mysite-111111.03.mdoq.io/media/foo.jpg - If you are happy with this, you can then commit this file to source control.
- Follow the normal deploy process to take the change to production. (If this is the only change you can do a zero downtime deployment)
- Once the change is on production, to apply it you just need to synchronize Nginx for the production instance.
(Within MDOQ select your production instance, click "Sync" > "Show More", check the "Nginx" component, click "Proceed".
Once the synchronization completes your new redirects will be active.
N.B MDOQ is only able to apply custom redirects for production sites hosted by MDOQ. If your site isn't hosted by MDOQ you can still test your rules on a development instance, but you will have to apply them to your production site manually (or with your current infrastructure deployment pipeline).