• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Adding canonical link in the head section (SEO)

Explorer ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

Hello there. I have a website with over 5 thousand pages there but none of them have cannonical links included. My question here is, is it possible to add something like: 

<link rel="canonical" href="##page_url##" />

Add this in the head section of html page. Now, here is the deal, will  this affect my SEO? Will this change apply only when the page loads?

 

Note: When I say page_url above, it's an actual URL page is connected with. If index.cfm page is on https://test.com/potato , then href of the cannonical should be exactly that. 

 

Views

828

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 05, 2024 Mar 05, 2024

I can offer a suggestion from the CFML technical perspective (I can't speak to the SEO-worthiness of your goal here, but others may have an opinion--or you can seek that info elsewhere). If your point is you want to cause the link tag to appear in the head tags of the page--but you are already past the point of the head having been created, there IS a CFML tag to help with that, called cfhtmlhead. Check out the docs for that.

 

And as for obtaining programatically the URL of the running page, th

...

Votes

Translate

Translate
Explorer , Mar 05, 2024 Mar 05, 2024

I did this and it seems to work:

 

<cfoutput>
    <cfset protocol = cgi.SERVER_PORT_SECURE ? "https" : "http">
    <cfset scriptNameWithoutIndex = ListDeleteAt(cgi.SCRIPT_NAME, ListLen(cgi.SCRIPT_NAME, '/'), '/')>
    <cfset fullURL = "#protocol#://#cgi.SERVER_NAME##scriptNameWithoutIndex#">
    <cfif len(cgi.QUERY_STRING)>
        <cfset fullURL = "#fullURL#?#cgi.QUERY_STRING#">
    </cfif>
    <link rel="canonical" href="#fullURL#" />
</cfoutput>

Votes

Translate

Translate
Community Expert ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

I can offer a suggestion from the CFML technical perspective (I can't speak to the SEO-worthiness of your goal here, but others may have an opinion--or you can seek that info elsewhere). If your point is you want to cause the link tag to appear in the head tags of the page--but you are already past the point of the head having been created, there IS a CFML tag to help with that, called cfhtmlhead. Check out the docs for that.

 

And as for obtaining programatically the URL of the running page, there is no single variable in CF that offers that, but see the cgi.script_name for thepath and filename, and cgi.query_string for any querystring, etc. Do a cfdump of the cgi scope to see other available fields. (And someone may propose some java code that can somehow obtain the full URL from the underlying servlet classes within CF.)


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

I did this and it seems to work:

 

<cfoutput>
    <cfset protocol = cgi.SERVER_PORT_SECURE ? "https" : "http">
    <cfset scriptNameWithoutIndex = ListDeleteAt(cgi.SCRIPT_NAME, ListLen(cgi.SCRIPT_NAME, '/'), '/')>
    <cfset fullURL = "#protocol#://#cgi.SERVER_NAME##scriptNameWithoutIndex#">
    <cfif len(cgi.QUERY_STRING)>
        <cfset fullURL = "#fullURL#?#cgi.QUERY_STRING#">
    </cfif>
    <link rel="canonical" href="#fullURL#" />
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2024 Mar 06, 2024

Copy link to clipboard

Copied

Good to hear, and glad to have helped get you started. Thanks for sharing your code. Others may propose refinements, but otherwise some people might benefit from it as-is.

 

I notice you're not choosing to use cfhtmlhead. Is it that you feel you can write this link tag always BEFORE any other html output is generated? You may want to consider using it anyway, to avoid unexpected problems related to that. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 06, 2024 Mar 06, 2024

Copy link to clipboard

Copied

Where can I use "cfhtmlhead" to achieve what you just said? I am not much familiar with all of this and ChatGPT helped me regarding this code above. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

LATEST

Did you read the docs link I offered? It shows an example, and you can use it anywhere you like. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

Confusing URLs for the same content? Fix it with a canonical link!

  • Tells search engines your "main" version of a page.
  • Goes in the <head> section of your HTML.
  • Use absolute paths and user-friendly URLs.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation