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
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,
, to the new pattern,
, their previous host, they could add
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
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]
As a result, visiting
gives us the resource on the server-side path
A RedirectMatch directive would look similar.
gives us a numbered match that is loaded into the corresponding variable
The first match becomes variable
, and so forth.
This rule will be applied to everything that looks like
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.