For some reason I need to keep both http and https versions of my site. I am using wordpress. Can I add a rel=canonical for https version through the .htaccess? What is the easiest way to avoid duplicate content?

1 Answer 1


Just set the canonical to the (preferred) HTTPS version. Both HTTP and HTTPS will still be available. Google will likely favour the HTTPS version over HTTP anyway, regardless of what protocol you set in the rel="canonical" tag (because it will presumably know that both HTTPS and HTTP are available).

Setting the canonical conditionally based on the protocol doesn't make sense - both HTTP and HTTPS are presumably serving the same content? You can't have two different "canonical URLs" for the same content.

UPDATE: If there is a code to set it using htaccess, please just let me know.

Whilst you can technically set the canonical URL by returning the appropriate Link: HTTP response header using mod_headers in .htaccess like this:

Header add Link "<https://example.com/url-path-to-page-goes-here>; rel=\"canonical\""

...I don't see how, on a WordPress site (or any site controlled by a CMS) you know what the canonical URL should be at the time .htaccess executes in order to be able to set the correct canonical URL? (Unless, perhaps, there is a specific URL pattern you follow? But this is not stated in your question.)

In my opinion, this needs to be done in WordPress, since only WordPress knows what the canonical URL should be.

  • Ok, I wanted to know how to set the canonical to https. One way is to set canonical to https version through Yoast SEO plugin for each url individually. I have heard it can also be done through htaccess. I already received a mail from webmasters regarding "duplicate without user-selected canonical". Jul 2, 2019 at 19:48
  • Hi Docroot, Can you give me the code for setting the preferred canonical version in htacces? Jul 2, 2019 at 20:11
  • Whilst you can set the canonical URL in .htaccess (by setting the Link: HTTP response header), I don't see how you know what the canonical URL actually is at that point in time (unless you have some very strict requirements for the URL structure?) in order to set the canonical header? Only WordPress knows what the canonical URL is supposed to be - which naturally runs long after .htaccess has processed the request. (?)
    – DocRoot
    Jul 2, 2019 at 21:12
  • "duplicate without user-selected canonical" - Are you sure that is related to just the difference in protocol?
    – DocRoot
    Jul 2, 2019 at 21:13
  • I also received a mail from cloudflare right now that a server disruption today caused a spike in 502 errors. In my webmaster account too, it is showing as a 5xx error. Plus the message was not for my preferred version which is non www but for the www version. I use non www urls. Jul 2, 2019 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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