How To Make Canonicals With PHP
I've search around on SO, but can't find an exact answer to my needs.
Generating a URL is pretty easy...
<link rel="canonical" target="_blank" rel="nofollow noreferrer" href="https://example.com<?php echo ($_SERVER['REQUEST_URI']); ?>" />
But, the issue with this is, the
$_SERVER['REQUEST_URI']) will always fetch the current file in use, so the canonical URL could potentially change.
So it could flick between www.example.com/hello.php and www.example.com/hello/, and many other variations depending on how the user accesses your site.
How do I make it so it's always the same url? (preferably without .php)
Worked it out myself, pretty basic:
<?php $fullurl = ($_SERVER['REQUEST_URI']); $trimmed = trim($fullurl, ".php"); $canonical = rtrim($trimmed, '/') . '/'; ?>
<link rel="canonical" target="_blank" rel="nofollow noreferrer" href="https://example.com<?php echo $canonical ?>" />
I'm sure there's different methods, but it works for me.
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?