LucyTurtle
LucyTurtle

Reputation: 1133

Laravel Redirect for Weird Encoded Space

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

Answers (2)

ficuscr
ficuscr

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

ahmed galal
ahmed galal

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

Related Questions