1

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?

LucyTurtle
  • 1,043
  • 1
  • 17
  • 37

2 Answers2

1

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.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • Hey! Thanks for your help. Your rewrite rule didn't work for me ): – LucyTurtle Jul 25 '19 at 14:37
  • Maybe explain what you mean by "didn't work". Failed to match? https://htaccess.madewithlove.be?share=17959635-52a3-521e-bc79-b3d0017fcfcc – ficuscr Jul 25 '19 at 17:12
  • Odd, I have it in my htaccess file and it doesn't seem to match since it's not getting rewritten – LucyTurtle Jul 25 '19 at 17:53
  • Order of execution? If you need to get into debugging that stuff... https://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite Hard to write the pattern from here. Would help to know what the actual URI is. Like hex values, not what you see in a web browser or copy and paste. I think my pattern works if URI has a trailing non-breaking space. – ficuscr Jul 25 '19 at 17:56
  • I actually just used this (thanks to your code) RewriteRule ^flannel-sheets\xc2\xa0$ /flannel-sheets? [L,R=301] And it worked! – LucyTurtle Jul 25 '19 at 17:58
  • Cool! I'm honestly not great with rewrites/regex so my confidence was waning ;) Crux is that `\xc2\xa0` part, sure you can adapt/build on it with some other examples. – ficuscr Jul 25 '19 at 17:59
1

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
 ); });

ahmed galal
  • 259
  • 2
  • 5
  • I don't think this pattern matches `[\s%20]`. Would working with a normal space though not NBSP. – ficuscr Jul 23 '19 at 21:48
  • Hey! Thanks for your help. Your rewrite rules didn't work for me ): I could catch all routes that's true, just wish there was an easier way. – LucyTurtle Jul 25 '19 at 14:37