In SEO, canonicalizing your URLs is very important. One of the first steps in this process is making sure all of your pages include (or exclude) www in your domain. This quick post will show you how to easily do this.
Table of contents
- Block 1 — protocols
- Block 2 — www
- Redirect to http and www
- Redirect to https and www
- Redirect to http and non-www
- Redirect to https and non-www
- Putting it all together
- http and www
- https and www
- http and non-www
- https and non-www
Add this to the beginning of your
.htaccess file (it might already be there):
RewriteEngine on is added, add two of the following blocks of code directly below it. If you have any
301 redirects, place them after whichever block you choose. None of the text below is a placeholder — in other words, don't change anything about the text. Any text that follows after the number sign (#) is a comment, and can be removed.
Block 1 — protocols
For the first block, we need to specify whether we want our domain to be accessed via http or https.
Redirect to http
Redirect to https
Block 2 — www
We've got everything redirecting to a single protocol, now we'll specify whether we want our domain to use www.