mcbetz
mcbetz

Reputation: 2399

How to strip special characters from URL via htaccess?

I have an URL https://link.de/sub/directory/• (that is %E2%80%A2 at the end) that should redirect to https://link.de/sub/directory/, hence I need to strip the character at the end.

I tried to solve it with a RedirectMatch:

RedirectMatch 301 ^directory.*$ https://link.de/sub/directory/

But I still get the at the end – which is a non-existing URL and produces an error.

How can I strip it from the URL and redirect to the URL without it?

Upvotes: 1

Views: 116

Answers (2)

anubhava
anubhava

Reputation: 786289

Translating my comment to answer.

You can use this RedirectMatch rule in your site root .htaccess:

RedirectMatch 301 ^/(sub/directory/).+$ /$1

Upvotes: 1

arkascha
arkascha

Reputation: 42984

I don't really see the issue here... You can simply "ignore" anything after the prefix you are interested in:

RewriteEngine on
RewriteRule ^/?sub/directory/ https://example.com/sub/folder/ [R=301,L]

And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Upvotes: 0

Related Questions