Home > Software engineering >  Canonical URL without trailing slash for file/path
Canonical URL without trailing slash for file/path

Time:12-23

I have a site based on Codeignitor(theinnovativepackaging.com). From SEO perspective I have read that trailing slashes in Root URL doesn't matter but it does matter for file/paths e.g /about-us and /about-us/ as far as I have studied about it. My problem is that my sites pages have trailing slash in URL but when you view page source and check if canonical URL matches but I doesn't see any trailing slash there. So does it matter if I have URL structure like this URL in address bar: example.com/product-name/ canonical URL: example.com/product-name. As far as I have studied its not good habit according to SEO. If it is ok then let me know or if its not kindly guide me how can I add trailing slash in product pages. I am attaching my sites .htaccess

 <IfModule mod_security.c>
  SecFilterEngine Off
  SecFilterScanPOST Off
</IfModule>

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]


RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1/$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

My main goal is to keep canonical tag and URL in address bar same if it really impacts SEO

CodePudding user response:

The value of the rel="canonical" link element (which I assume is what you mean by the "canonical tag") should contain your canonical URL. If you are linking/using a different URL in your internal links then you are misrepresenting the canonical URL in your "canonical tag".

Yes, the two should be the same. So, you either need to append the trailing slash in your "canonical tag", or remove the trailing slash in your internal links.

If you are consistently linking to the URL with the trailing slash then Google will likely ignore your "canonical tag" that omits it.

I am attaching my sites .htaccess

Your .htaccess file would seem to be irrelevant here. You are not forcing (or removing) the trailing slash here. (You could later add a canonical redirect to force one or the other, should the non-canonical URL be requested.)

However, your immediate problem is not something you would resolve in .htaccess anyway. You need to correct your internal links OR the "canonical tag".


UPDATE:

I am having this link in my template file <link rel="canonical" href="<?php echo current_url(); ?>" /> It is appending slash with root URL but not appending any slash for other pages. Ca you suggest me some logical condition to write here so that slash gets appended with other pages too

You could perhaps do it like this:

<link rel="canonical" href="<?php $url = current_url(); echo preg_match('/\/$/',$url) ? $url : $url.'/'; ?>" />

This will append a trailing slash to the URL if one is not already present. Although I'm sure there is a more Codeigniter way of doing this. (?)

  • Related