I’ve search around on SO, but can’t find an exact answer to my needs.
Generating a URL is pretty easy…
Like so:
<link rel="canonical" 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)
4
Answers
Worked it out myself, pretty basic:
Then...
I'm sure there's different methods, but it works for me.
This is what i do.
I think your scripts require a bit of sanitisation, am I right?
I mean, if your page is
but a malicious – but harmless – person does
your canonical would become
you wouldn’t want that to happen, though, am I right? Especially if the malicious person is not harmless and does worst things with your urls…
This will remove query parameters like
?search=abc&page=32
Option 1:
Option 2 (does the same) :
Then echo