DEV Community

Liam Gomez
Liam Gomez

Posted on

Cloudflare Domain Migration Keeping Slug

Why?

We need to migrate a service over to a new URL structure, this has significant SEO benefits. This can increase our traffic and our sites ranking straight to the top. Google feeds of getting rooted domains, and serving these as top results, let alone if its a ".com" domain.

What?

At the end of the day what we want to be able to achieve is migrating the service from one subdomain to the root domain. Doing with with ease and as little customer impact as possible. We want to also migrate our URL structure from serving /en to serving /en-gb /en-es - Essentially serving both country code and the language we want to serve the service with.

Who?

This is related to people who want to accomplish a migration when using Cloudflare as their CDN. Cloudflare is a great service and offer some amazing tools that can help with the and to solve other solutions.

My way of solving this issue.

Since we are already using Cloudflare as our CDN we can simply redirect the traffic that goes from to our service.example.com/en and point it to www.example.com/en-gb/ - This being said we realistically want to serve the same content seamlessly. We take the example of a customer having their particular favorite part of their site.

A customer saves service.example.com/en/wonderful/service-a - We need them to move to the same service on the new domain with the new folder structure. New route would look like: www.example.com/en-gb/wonderful/service-a

Using Cloudflare Rules > Redirect Rules.
Image description

So as you can see this is a simple rule where we check for a particular URL to make sure that if a customer or some traffic goes to this particular subdomain then we want to do something with this customer/ traffic.

We then move along to so some extra checks in order to move this traffic to the new subdomain with the correct url structure.
Image description

With the above essentially take the traffic and we look at this, we extract the uri path that the user has used - if the customer originally used service.example.com/en/service/1234 we take this request and extract the URI Path [ /en/service/1234 ] - the further take this URI Path using a regex replace and remove the first instance of [ /en ] which leaves us with [ /service/1234 ] - This works out well as now all we need to do is add a prefix to this URI Path. In this case as you can see we add [ https://www.example.com/en-gb ] - The final result of all this will equal [ https://www.example.com/en-gb/service/1234 ] and sends a 302 status code within the redirect to let any bots know that this is where the new content for that old domain lays so that it can now be indexed and keep its score.

Hopefully this was helpful!

Top comments (0)