Reputation: 1133
It seems we have some weird broken incoming links for our website that we would like to redirect. We are using Laravel on Apache.
Here is the incoming url: /flannel-sheets%C2%A0
Here is where we want to send it: /flannel-sheets
%C2%A0 seems to decode to a strange space.
I have tried these lines in the web.php
file
$this->get( 'flannel-sheets%C2%A0', function() { return redirect( '/flannel-sheets' ); });
$this->get('flannel-sheets ', function() { return redirect( '/flannel-sheets' ); });
$this->get('flannel-sheets$nbsp;', function() { return redirect( '/flannel-sheets' ); });
I have also tried using Apache redirects in my 000-default.config
and .htaccess
files
Redirect 301 /flannel-sheets%C2%A0 https://example.com/flannel-sheets
Neither of these methods are working for me. What should I do?
Upvotes: 1
Views: 611
Reputation: 7063
So it is a non-breaking space (0xC2 0xA0).
Annoying to deal with these once they are in the wild, bad inbound links that is.
Using mod_rewrite
is probably the best option here. Something like this would work...
RewriteRule ^(.+)\xc2\xa0$ $1 [L,NE]
You'll want to check for those characters, not their URL encodings. I'm wondering if mod_speling
offers anything useful for this scenario... not seeing anything so far.
Upvotes: 1
Reputation: 269
you can fix it using :
# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
# replace spaces by - in between
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
or you can receive the broken routes dynamically
$this->get( '/{route}', function($route) {
//filter $route the way you need
); });
Upvotes: 1