3

I have been receiving lots of canonical error messages from Google Search Console. Most of the time, the referring pages are my own pages and the urls are correct in the referring page.

Google is indicating that the errors are destination urls without a trailing slash. I.e, example.com/mypage vs example.com/mypage/ However, my permalink structure in WordPress is set to end all urls with a trailing slash. The user declared canonicals are also using trailing slashes according to the page source and Google.

I think the problem is with my htaccess file and a WordPress plugin that has enabled https rewrites. My current htaccess rewrite rule is below.

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

I'm not a regular expression or htaccess expert, but if I want my permalinks to all end with a slash shouldn't this be:

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [R=301,L]

I.e., shouldn't there be a "/" after the $1?

Edit If I type in http:example.com/mypage in the address bar, it still redirects to https://example.com/mypage/.

Plugin Rewrite section (comes first in htaccess file):

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Wordpress Rewrite section (comes later in htaccess file):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Note: After looking at my network tab (as MrWhite suggested) I see only one 301 redirect (not two). I.e., http://example.com/mypage is redirected to https://example.com/mypage without the trailing slash. Additionally, if I use https://example.com/mypage it redirects to https://example.com/mypage without the trailing slash as well even though the url is identical.

Mr White, you are correct, I am using Cloudflare and my SSL is set to strict on Cloudflare.

Request/Reponse Headers:

GET /mypage HTTP/2
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
...
Upgrade-Insecure-Requests: 1
...
Pragma: no-cache
Cache-Control: no-cache
TE: trailers

HTTP/2 301 Moved Permanently
...
location: https://example/mypage/
strict-transport-security: max-age=63072000; includeSubDomains;preload
...
x-redirect-by: WordPress
...
server: cloudflare
...
X-Firefox-Spdy: h2
2
  • Is the redirect from /mypage to /mypage/ triggered by a rule in .htaccess or by WordPress? Check the network traffic to make sure there is not a (lengthy/incorrect) redirect chain. (Although if the redirect to append the trailing is performed by WP then you would see 2 redirects... the first to HTTPS and the second to append the trailing slash. But this is not necessarily wrong.)
    – MrWhite
    Mar 15, 2023 at 22:38
  • "if I use https://example.com/mypage it redirects to https://example.com/mypage without the trailing slash as well even though the url is identical." - Are you actually seeing an external 3xx redirect? What is the HTTP status? What are the HTTP response headers on these requests?
    – MrWhite
    Mar 16, 2023 at 21:28

1 Answer 1

2
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

There's nothing wrong with this rule. (Although it's not complete by itself. I assume there must be a RewriteCond directive that precedes this that checks the HTTPS status.)

The purpose of this rule is to simply redirect from HTTP to HTTPS, preserving the same URL-path (and hostname) as requested. Yes, it would redirect http://example.com/mypage to https://example.com/mypage. But it also redirects http://example.com/mypage/ to https://example.com/mypage/. If you simply appended a trailing slash to the substitution string then it would redirect /mypage/ to /mypage// - which would not be good.

If you wanted to redirect from no-trailing-slash to trailing-slash in .htaccess then this should be implemented as a separate rule. This rule can also redirect to HTTPS in order to potentially avoid multiple redirects when the HTTP version of the URL (without a trailing slash) is requested - but this should be an edge case.

The problem is unlikely to be in the .htaccess file, unless there is a redirect that removes the trailing slash - although that does not seem to be the case.

You need to make sure that:

  1. All your internal links point to the canonical URL with a trailing slash.
  2. All links in the sitemap reference the canonical URL.
  3. All rel="canoncial" elements reference the canonical URL. (As you mentioned.)

Maybe the GSC error is historical? Did you previously use URLs without a trailing slash?

Also check the network traffic for any redirect chains. Whilst the destination URL contains the trailing slash, maybe there is an intermediary redirect that removes it?!

3
  • Thank you @MrWhite. I added more details from my htaccess file to the OP and I checked the Network tab for multiple redirects. I'm only seeing an initial redirect in my browser. It's redirecting to https without the trailing slash, but then the next line is 200 with the trailing slash, so I'm not really sure how WordPress is adding the slash as I thought it was via htaccess.
    – Trebor
    Mar 16, 2023 at 18:36
  • @Trebor It would seem you are using Cloudflare? Do you have any page rules defined? CF can also be configured to redirect HTTP to HTTPS (no need for .htaccess). Please update your question with the HTTP response headers for each request. Specifically, what are the Location and Server headers in these responses?
    – MrWhite
    Mar 16, 2023 at 21:34
  • Thank you. I think you've located the problem. I did migrate to Cloudflare, and recently turned on Strict SSL. I would not have thought that my WP, my plugin and Cloudflare were causing the problem together. Am I to assume then that if I remove the SSL redirect added by my plugin, and leave Cloudflare in place with SSL, this should solve the redirect? Thanks. BTW, how did you recognize Cloudflare usage from my post?
    – Trebor
    Mar 17, 2023 at 18:10

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.