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 of several problems like absence of redirection, website being accessed from the version http, multiple redirect errors, and many others.

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

Today I went to get SSL from my site on httpstatus.io and I came across two redirect 301 in my version with www and without https from the website. It may seem insignificant but it is strictly important for the SEO of your website.

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 it is quite simple. I use Cloudflare, so I accessed my panel and domain in CloudFlare and went to the tab Page Rules.

There I created a unique version redirect www straight to version https without www. This rule besides solving the problem most of the time rules out the need to use any plugin or redirection rule on the server nginx or in .htaccess of apache.

When accessing Page Rules, just create a new rule redirecting www.mysite.com/* for 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 version https from that page instead of redirecting to the homepage.

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 no WWW redirect. If you liked it, share it with friends, don't let their website have double redirect and harm SEO their.