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.
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
...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>
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.)
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>
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.
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.
Copy link to clipboard
Copied
Did you read the docs link I offered? It shows an example, and you can use it anywhere you like.
Copy link to clipboard
Copied
Confusing URLs for the same content? Fix it with a canonical link!