WWW Redirect on Cloudflare

Having trouble with dual 301 redirect on your WWW domain? Are some pages not redirecting from HTTP to HTTPS? In this article I will give you a quick tip on how to solve this problem.

This function is the solution to various problems such as the absence of redirection, the site being accessed from the http version, multiple redirection errors, and many others.

Double Redirect 301 at [http: // www.]

Today I went to configure the SSL for my site on httpstatus.io and came across two 301 redirects on my version with www and without https of the site. It may seem insignificant, but it is critically important for your site's SEO.

As you can see in the image below, the website URL with HTTP: // WWW has two 301 redirects, probably one for HTTPS version and one for non-WWW version of the website.

Redirecionamento WWW no Cloudflare

To solve this problem is quite simple. I use Cloudflare, so I accessed my dashboard and domain on CloudFlare and went to the Page Rules tab.

There I created a unique redirect from the www version directly to the https version without www. This rule not only solves the problem but often eliminates the need to use any plugin or redirect rule on the nginx server or in the .htaccess of apache.

When accessing Page Rules, simply create a new rule redirecting www.meusite.com/* to https://meusite.com/$1 using the 301 redirect rule.

The values [*] and [$1] are important because they identify which page the user is entering and redirect directly to the https version of that page instead of redirecting to the home page.

See how I set up my domain below. Do the same then click save and clear your CloudFlare cache. Thus the double redirect will cease to exist, leaving only one 301.

Redirecionamento WWW no Cloudflare

We also recommend reading our Guide on SEO and Tools.

Redirection by .HTACCESS file

The same redirection rule can be applied directly from the server, if you do not use Cloudflare. Although we strongly recommend using a CDN and doing so on it.

The rule to be applied to the .HTACCESS file will be below. Remember, only do this if you are not using Cloudflare.

<IfModule mod_rewrite.c>
   RewriteCond %{HTTPS} off [OR] 
   RewriteCond %{HTTP_HOST} ^www\.seusite\.com [NC] 
   RewriteRule (.*) https://seusite.com/$1 [L,R=301]
</IfModule>

Redirecting WWW on Nginx

If your server is purely Nginx like mine, without a doubt it is much easier to use CloudFlare and the first tutorial. In case you are still stubborn, I will leave a configuration for Nginx, but I will not spend my time explaining it, since I am not a Nginx Config professional:

#=========================#
# domain settings #
#=========================#

# Catch http://domain, and http://www.domain
server {
        listen 80;
        server_name www.domain domain;

        # Redirect to https://domain
        return 301 https://domain$request_uri;
}

# Catch https://www.domain
server {
        listen 443;
        server_name www.domain;

        # Redirect to https://domain
        return 301 https://domain$request_uri;
}

# Catch https://domain
server {
        listen 443;
        server_name domain;

        root /usr/share/nginx/domain;
        index index.html index.htm;

        ssl on;
        ssl_certificate /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
        ssl_prefer_server_ciphers on;

        location / {
                try_files $uri $uri/ =404;
        }
}

I hope the tips in this article solve your problem with double redirection or the absence of WWW redirection. If you liked it, share it with your friends, don't let their site have double redirection and harm their SEO.