Imagine you changed the navigational structure of your Metro Publisher instance, but you don't want your website visitors to run into 404 errors every time they try to access any of the pages you moved by their old URL. This is when redirects come in handy. A redirect is a method to tell web browsers and search engines that a page or a website has been moved to another location. Metro Publisher supports 301 redirects which means that the page move is considered to be permanent.
We are going to add a simple redirect which redirects a single URL to another, and we will also present a couple of examples which will demonstrate how to create redirects for more complex use cases.
To add a redirect, we send a PUT request to the redirects resource /{iid}/redirects/.
This resource allows us to create a new redirect and set its basic information: the pattern, template and ord attributes.
>>> PUT("/123/redirects/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
... {'pattern': '/rome-report/',
... 'template': '/Vacations/great-vacation-in-rome-trip-report/',
... 'ord': 1})
{'url': 'https://api.metropublisher.com/123/redirects/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
'uuid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'}
The Metro Publisher API allows you to use placeholders in order to define a pattern you would like to redirect. That way you can define redirect rules which apply to a whole set of URLs which means you don't have to add one redirect for each single URL.
The following example shows how you can redirect all URLs that follow this pattern: BASE_URL/Vacations/Rome/article-url-name/ to: BASE_URL/Vacations/Trip_Reports/article-url-name/
>>> PUT("/123/redirects/dddddddd-dddd-dddd-dddd-dddddddddddd",
... {'pattern': '/Vacations/Rome/**',
... 'template': '/Vacations/Trip_Reports/\\1',
... 'ord': 2})
{'url': 'https://api.metropublisher.com/123/redirects/dddddddd-dddd-dddd-dddd-dddddddddddd',
'uuid': 'dddddddd-dddd-dddd-dddd-dddddddddddd'}
The next example lets you redirect all URLs that mach this pattern: BASE_URL/article_data/subsection-url-name/article-url-name/ to: BASE_URL/subsection-url-name/article-url-name/
>>> PUT("/123/redirects/eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee",
... {'pattern': '/article_data/*/**',
... 'template': '/\\1/\\2',
... 'ord': 3})
{'url': 'https://api.metropublisher.com/123/redirects/eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee',
'uuid': 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee'}
And the final example lets you flip the order of your URL pattern's elements. All URLs that match BASE_URL/section-1/subsection-1/article-url-name/ will be redirected to BASE_URL/subsection-1/section-1/article-url-name/
>>> PUT("/123/redirects/ffffffff-ffff-ffff-ffff-ffffffffffff",
... {'pattern': '/*/*/**',
... 'template': '/\\2/\\1/\\3',
... 'ord': 4})
{'url': 'https://api.metropublisher.com/123/redirects/ffffffff-ffff-ffff-ffff-ffffffffffff',
'uuid': 'ffffffff-ffff-ffff-ffff-ffffffffffff'}