How to implement 301 redirect with parameter passing in CloudFlare?

Technology

Recently, one of Mingyue’s client websites changed its domain name and needed to implement a 301 redirect between the old and new domain names to deliver SEO effects. This required that all links on the site support automatic redirection to the new domain name, which means a 301 redirect that can pass link parameters.

Previously, this could be achieved by simply writing a rewrite statement in Nginx, but this customer has already connected the entire site to CloudFlare, so naturally, the redirection must be implemented in CloudFlare. Mingyue feels that the effect in CloudFlare will be better than that in Nginx. Today Mingyue will share with you the 301 redirection configuration of CloudFlare.

Generally speaking, websites need 301 redirects because of domain name changes. This is also the most common redirection. Anyway, the meaning is to realize that the b.com domain name automatically jumps to a.com. If it is a jump between the old and new domain names, the parameters must be automatically passed, such as: b.com/help.html automatically jumps to a.com/help.html. The purpose of doing this is mainly for SEO inheritance. Mingyue has tested that Google and Bing both support SEO transmission of 301 redirection. In CloudFlare, as long as your domain name is connected to the DNS, you can easily implement 301 redirection and choose whether to pass parameters.

With the revision and update of CloudFlare backend, many backend settings have been clearly categorized. If we want to redirect, we can create rules in CloudFlare backend – [Rules] – [Redirection Rules]. The free version of CloudFlare supports 10 [Redirection Rules], which are not enough for many of our websites. If we want to redirect the b.com domain to a.com, we only need to create the following rules in the b.com domain:

Because it is just a redirection between domain names, the [Static] target is selected here. You only need to fill in the target accessible URL. Note that the status code after it must be 301 (permanent redirection) or 308 (permanent redirection) so that search engines can pass SEO. 302 or 307 are temporary redirections!

If you change the main domain name of the website, you will need a redirect that can pass parameters. For example, if we change the website domain b.com to a.com, because the website architecture and content are the same, the URL parameters must be the same. In order to let the search engine search results point to the new domain name as soon as possible, you must have a redirect that can pass parameters. You only need to create a rule as shown in the following figure in the CloudFlare background – [Rules] – [Redirect Rules]:

Note the difference in the details in the above picture. To pass parameters, it must be a [dynamic] target, and the [expression] cannot be just a URL, but:

concat("https://a.com", http.request.uri.path)

In fact, it is to fill in the target URL and request to process the parameters passed. The target URL must be based on the actual normal access URL. In our example, the target URL is https://a.com. If your target URL is https://www.a.com, you must change it to https://www.a.com, otherwise there will be an error of too many redirects. This detail is easy for many novices to make mistakes.

Finally, you must check the [Keep query string] below to pass the parameters to the target URL for processing, remember!

OK, the domain name redirection setting that CloudFlare can pass parameters is complete. Mingyue has tested that the redirection response speed in CloudFlare is still very fast, and the support of various mainstream search engines is also very good. Google and Bing recognition usually take effect within 24 hours. Of course, if you want to try temporary redirects such as 302 and 307, it is also fine. It is still very cool to use them in some scenarios. Mingyue likes to temporarily redirect malicious requests, malicious scans, CC attacks, malicious fast rankings, etc. to other URLs, haha! ! ! Other URLs, haha! ! !

1 thought on “How to implement 301 redirect with parameter passing in CloudFlare?

Leave a Reply

Your email address will not be published. Required fields are marked *