Mobile app version of vmapp.org
Login or Join
Alves908

: SEO - does Google read dynamic canonical tags? I used the following code to redirect all the HTTP pages of a site to the https:// version. <link rel="canonical" href=""> <script>

@Alves908

Posted in: #CanonicalUrl #Dynamic #RelCanonical #Seo

I used the following code to redirect all the HTTP pages of a site to the version.

<link rel="canonical" href="">
<script>
var url = window.location.href; // get current URL
var link = document.querySelector('link[rel=canonical]'); // get the link element
var changedUrl = [url.slice(0, 4), 's', url.slice(4)].join(''); // change the URL to link.setAttribute('href', changedUrl); // set href attribute of link element
</script>


Now my questions is, will Google read the dynamically generated canonical tags and index them like normal canonical tags? Or will I have to try another approach?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Alves908

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

If your objective is to rename http into https do not try to do this with canonical URLs. You should NOT have 2 sites with the same content (that is how Google will view your https/http). You go to your provider (hosting) panel and make a 301 to the HOME page as and then set up a Google Search Console for with-and-without-www.com. Next you need to add the .htaccess redirect code for www or not and https instead of http.



If your objective is SEO

and you can not use a Yoast (or similar) tool then MAYBE this link seems a possible solution to AUTO-canonical URL insertion: github.com/missiondata/google-analytics-code-inserter-perl-script
OR: css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
OR: www.simoahava.com/seo/dynamically-added-meta-data-indexed-google-crawlers/

10% popularity Vote Up Vote Down


 

@Eichhorn148

yes. but it depends. from how fast you insert it. i tested some lazy load scenarios with js and realized, that in google's cache are afterloads visible, which happens even after 5 seconds after onLoad is fired. btw, you will not be able to see it in the search console with fetch as google - fetch as google is only a kind of preview. i know cases, where even not the whole page was shown in fetch as google, because it was too long:)

but don't stretch google's patience - try to make loadtime as fast you can - in reality you never know, how long it will wait. and all tests we can perform have a kind of pretty limited validity, because we don't know, what factors, beside of those we control, are in this game.

if i were you, i would insert canonicals into the header with x-robots-tag - it can be done dynamically, and the tag is already inserted, if google reads the page.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme