0

After adding an SSL certificate I want to 301-redirect all my site's URL variations to non-www ones. I tried creating a redirect from cPanel for all other variations.

Every other URL variations redirect except for https://www.example.com, this one does not redirect to https://example.com. Both URLs can be accessed and won't redirect to the non-www ones.

I've added canonical, Google has crawled and indexed both. Analytics gave a redundant hostname notification. I also tried redirecting from an htaccess file using these code:

# BEGIN SSL
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ https://example.com$1 [R,L]
# END SSL

and

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Both failed.

All I want is to redirect all URL variations, secure or non-secure, to https://example.com (non-www secure version).

2
  • Are directives in .htaccess files enabled with an AllowOverride directive in your server or virtual host config? – Éric May 25 '20 at 10:25
  • Incidentally, be aware that the default value for the R redirect flag is 302. In your case, be sure to state R=301 for each redirection, for your canonical URL to enforce quickly. – Éric May 25 '20 at 14:47
1

Your first code has 2 errors in the RewriteRule directive:

  • ^$ does not capture anything, so $1 is always empty here.
  • A slash is missing before $1, because if it could have captured something like Foo.html, the substitution URL would be of the form https://example.comFoo.html.

Your second code seems valid to me, although the RewriteBase directive is useless since your are using an absolute substitution URL. I do not understand why it does not redirect www-URLs to secured non-www URLs. Something must be interfering with this code.

Finally, in your case, for redirecting every alternative URLs to the secured non-www URLs, an adequate set of Apache directives for an .htaccess file could be:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://example.com/$1 [R=301,L]

Make sure that some of your cPanel redirection settings do not interfere with these directives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.