Forum Topic: Canonicalization for https?

Forum: .htaccess Forum : WordPress • Posted by Julian • Updated:

Hi, I have recently added ssl to my site and was wondering if this then creates another duplicate content issue as with http://example.com and http://www.example.com. Do I now need to also worry about https://....???

Currently I am using:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

with this htaccess code it allows me to type:

example.com/about and it redirects me to www.example.com/about/ and displays my page.

After purchasing the book I wanted to try out some of the examples and so I tried to use:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301,L]

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

RedirectMatch 301 ^/tag/$ http://www.example.com/
RedirectMatch 301 ^/search/$ http://www.example.com/
RedirectMatch 301 ^/category/$ http://www.example.com/

but this htaccess makes it that when I go to example.com/about it cannot find the page and then takes me to my ISP's search engine.

Is there something I am doing wrong?

Thanks in advance for any replies!

5 Replies to “Canonicalization for https?”

Jeff Starr
Posted by Jeff Starr

Hi Julian,

I recently encountered the same issue and then remembered that WordPress redirects your site to whatever you specify under Settings > General, for the site/blog URLs. That is, if you include the www prefix there, WP will redirect to it.

At least, that's what did it for me!

Posted by Julian •

Hi Jeff, thanks for your reply however I am not sure I understand so if possible please could you expand upon it.

My questions were:

1. I have recently added ssl to my site and was wondering if this then creates another duplicate content issue as with http://example.com and http://www.example.com. Do I now need to also worry about https://?.???

2. Currently my site uses the standard htaccess file of:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ ? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

with this htaccess code:

www.example.com/about

is redirected to:

www.example.com/about/

and this displays my page. So if the user types either the result is successful.

On purchasing the book I wanted to try out some of the examples and so I copied and pasted a couple and tried the following htaccess:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1$1 [R=301,L]

however with this code the url:

www.example.com/about

is NOT redirected to /about/ it takes me to my ISP's search page thus generating an error for me. Is this normal?

Sorry for the very long winded question!!!!

Jeff Starr
Posted by Jeff Starr

Hi Julian,

For the first question, the server should be handling the redirects automatically, so unless you are seeing errors I wouldn't worry about. But just to be safe, you may add this to .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

For the second question, are you asking about the missing trailing slash? Just want to make sure before I offer any code..

Posted by Julian •

Thanks Jeff I will give that a try.

For the second question yes I am asking about the trailing slash, currently without editing my htaccess it handles the no trailing slash by redirecting to the trailing slash but when I add to my htaccess code it no longer works that way.

Is it also possible for me to direct message you about another issue?

Jeff Starr
Posted by Jeff Starr

For the trailing slash, it may be possible using a "negative lookahead", like so:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)(?!/)$ http://www.%1$1/ [R=301,L]

What this does is match everything but the trailing slash, which it then adds to the target URL to ensure that it's there.

I haven't tested this, so additional tweaks may be needed.

Yes, feel free to contact me any time via email: https://perishablepress.com/contact/