3

Google Search Console is giving me the Duplicate without user-selected canonical error for URLs that contain an unnecessary double slash // in them, like:

https://university.edu/~user//dir/

To avoid defining a canonical URL in the head of every index.html file on the site, it would be nice to just add a URL rewriting rule in my .htaccess file that would redirect all URLs with such a double-slash (not the initial double-slash after the https: though) to the corresponding URL with a single slash. Is there are rewrite rule that would do this? And would this appease Google Search Console?

1
  • 1
    It looks like you are using Apache per-user web directories, in which case. the double slash you are seeing is effectively at the start of the URL-path you would normally define in your application when constructing internal anchors/URLs. So you should check your HTML/code to see how your internal links are constructed.
    – MrWhite
    Dec 17, 2023 at 20:04

1 Answer 1

2

In most cases, it is better to create a redirect instead of setting a canonical tag, especially if these are really just technical duplicates.

I would also recommend finding out how Google knows about the duplicates. If the cause is on the website itself, it should be fixed also.

Here is an example rule for rewriting duplicate slashes in URLs to a single slash (the slashes after the https: are ignored). Please test the rule yourself for possible conflicts with other rules already defined in the .htaccess.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
1
  • 1
    "duplicate trailing slashes" - the rule removes "duplicate slashes" anywhere in the URL-path, not just "trailing" slashes (that occur at the end of the URL-path). I've removed the 3 instances of "trailing" from your answer. (+1)
    – MrWhite
    Dec 17, 2023 at 20:04

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.