Taco Steemers

A personal blog.
☼ / ☾

Creating a new website without breaking search engine results and bookmarks

When moving our website to a different CMS or framework we might end up with a different URL structure. This is bad for search engine rankings and people's bookmarks. To avoid problems we can use rewrite rules and redirect rules.

When moving our website to a different CMS or framework we might end up with a different URL (web address) structure. This is bad for search engine rankings and people's bookmarks. To avoid problems we can use rewrite rules and redirect rules.

An acquaintance asked for help with a redirect rule on LinkedIn. They explained that their new website design used different software that placed their blog articles in a different directory, resulting in a different URL. The change in URLs was not acceptable to them because they did not want to lose the SEO profile (search engine rankings) for their website. If a search engine cannot find your pages anymore, your pages lose their ranking in the search results. They thought they needed a redirect rule to redirect the search engine bots from the old URL pattern, /YYYY/MM/slug , to the new pattern, /posts/YYYY/MM/slug . On Netlify , their previous host, they could add /:year/:month/:slug /posts/:year/:month/:slug to their _redirects-file to achieve the redirect effect. Their new host uses the Apache web server, a very common choice. They wondered how they could achieve the same effect with Apache. The Apache documentation has a great page on this topic .

Sequence diagram for the redirect model

It is clear what they want to achieve, and it can be done with the RedirectMatch directive , but they do not need to use a redirect. A redirect requires clients and search engine bots to actually visit the old address before they are redirected to the new address. That takes a small amount of time. Humans prefer low load times. Depending on the contents and architecture of your site, a single page on your site might load twenty resources such as fonts, styling documents, javascript, images, pieces of advertising copy, contact details and finally the actual content. In the end all small load times add up to a bigger, more noticeable load time. Redirects may also be applied on top of each other. Mistakes can lead to wrong or infinite redirects. This problem might suddenly occur when you want to take the website down for maintenance and redirect all URLs to an "under maintenance" page.

An alternative solution is possible with a RewriteRule directive . This solution does not use redirects towards the clients but instead tells Apache how to fulfill the request, internally. The following example keeps the URLs the same, regardless of the change in directory structure:

RewriteEngine On 
RewriteRule ^([0-9]+)/([0-9]+)/(.+)$ posts/$1/$2/$3 [NC,L]
Sequence diagram for the rewrite model

As a result, visiting /1999/01/slug gives us the resource on the server-side path /posts/1999/01/slug . A RedirectMatch directive would look similar. () gives us a numbered match that is loaded into the corresponding variable $ . The first match becomes variable $1 , and so forth. This rule will be applied to everything that looks like numbers/numbers/any_character . It can be improved by indicating that the first match should have four numbers (for the year), and the second match should have two numbers (for the month). The rule as is only specified + , 'one or more'. The final square brackets give two more instructions. NC indicates that the rule is not case sensitive. L indicates that if this rule matches the request it should be the last to be applied. In other words, this finalizes the rewriting portion of the request handling process.