Reputation: 567
I had some pages that were showing as broken links on Google Search console, one of them being...
https://evolvefitness.co.uk/personal-trainer-london/
... the URL that does exist is... https://evolvefitness.co.uk/locations/personal-trainer-london/ ... so I added this redirect to my htaccess file...
RewriteRule ^personal-trainer-london/? https://evolvefitness.co.uk/locations/personal-trainer-london/ [R=301,L]
...this worked great, however when I visited a page that included the first portion of the redirected URL, that also got redirected...
https://evolvefitness.co.uk/personal-trainer-london-landing/
I've turned all the redirects off now, but how would I tell the redirect only to happen on an exact URL? Is it something to do with the question mark at the end thats doing it? i've looked at some documentation but got completely lost
Would these play some part in it too?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^evolvefitness.co.uk
Thanks
Upvotes: 0
Views: 15
Reputation: 42885
I assume this is what you are looking for:
RewriteRule ^/?personal-trainer-london/?$ https://evolvefitness.co.uk/locations/personal-trainer-london/ [R=301,L]
The trailing $
in the matching pattern anchors to the line end (here the URL end). That means that only exact matches are considered, not requests to URLs where something follows that pattern.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Upvotes: 1