How to Add Canonical URLs in WordPress Without Plugin + Remove Query String From Canonical URLs

The canonical URL is crucial for SEO. To create canonical URLs in WordPress without a plugin, follow these steps.

If you do not want to read the details on how canonical URLs are generated in WordPress, here is our full code. Paste this code before closing </head> tag in your header.php.

For single posts, pages use this code:

<?php if ( is_singular() ) { ?> <link rel="canonical" href="<?php the_permalink(); ?>" /> <?php } ?>

For archives such as: categories, tags or any other taxonomy use this code:

<?php if ( is_tax() || is_archive() || is_home() || is_category() ) { ?> <link rel="canonical" href="https://www.yourdomain.com<?php echo $_SERVER['REQUEST_URI'];?>"> <?php } ?>

There are actually two codes here. one for archives and one for individual posts and pages. Both codes could be combined into one code by someone with advanced PHP expertise.

Both of the codes were created to be utilized across the entire website. However, some things are ineffective.

Although the first code is fairly sound, there is a flaw. This code creates the initial post link rather than the archive link in the posts list.

Even the second code is good, but it also has a flaw that, regrettably, we haven’t totally fixed. When the address is entered into the address bar, this code generates a canonical URL.

Example: If you enter www.domain.com/my-post/// in the adress bar, then the canonical URL is generated as it is written with multiple slashes.

That’s why we modified both codes and put two instead of one.

Clarification: This tutorial is written based on experiments with our themes and servers. If these codes do not work well for us, we cannot say that they do not work on other servers and that  who created them did not write codes correctly.

How to remove the query string from Canonical URL and get only the URL?

If you have same content with different query strings you can add strtok($_SERVER[“REQUEST_URI”], ‘?’); to remove query string and get the original URL.

I’m using PHP to build the URL of the current page. Sometimes, URLs in the form of

www.example.com/myurl.html?unwantedthngs

are requested. I want to remove the ? and everything that follows it (querystring), such that the resulting URL becomes:

www.example.com/myurl.html

Solution For Removing Query String from Canonical URL

You can use strtok to get string before first occurence of ?

$url = strtok($_SERVER["REQUEST_URI"], '?');

strtok() represents the most concise technique to directly extract the substring before the ? in the querystring. explode() is less direct because it must produce a potentially two-element array by which the first element must be accessed.

Some other techniques may break when the querystring is missing or potentially mutate other/unintended substrings in the url — these techniques should be avoided.

A demonstration https://3v4l.org/fRNSk:

$urls = [
    'www.example.com/myurl.html?unwantedthngs#hastag',
    'www.example.com/myurl.html'
];

foreach ($urls as $url) {
    var_export(['strtok: ', strtok($url, '?')]);
    echo "\n";
    var_export(['strstr/true: ', strstr($url, '?', true)]); // not reliable
    echo "\n";
    var_export(['explode/2: ', explode('?', $url, 2)[0]]);  // limit allows func to stop searching after first encounter
    echo "\n";
    var_export(['substr/strrpos: ', substr($url, 0, strrpos( $url, "?"))]);  // not reliable; still not with strpos()
    echo "\n---\n";
}

Output:

array (
  0 => 'strtok: ',
  1 => 'www.example.com/myurl.html',
)
array (
  0 => 'strstr/true: ',
  1 => 'www.example.com/myurl.html',
)
array (
  0 => 'explode/2: ',
  1 => 'www.example.com/myurl.html',
)
array (
  0 => 'substr/strrpos: ',
  1 => 'www.example.com/myurl.html',
)
---
array (
  0 => 'strtok: ',
  1 => 'www.example.com/myurl.html',
)
array (
  0 => 'strstr/true: ',
  1 => false,                       // bad news
)
array (
  0 => 'explode/2: ',
  1 => 'www.example.com/myurl.html',
)
array (
  0 => 'substr/strrpos: ',
  1 => '',                          // bad news
)
---

https://www.indexofapps.com/how-to-generate-canonical-urls-in-wordpress-without-plugin/

https://stackoverflow.com/questions/6969645/how-to-remove-the-querystring-and-get-only-the-url

Related Posts