0

I'm trying to remove certain characters from old URLs upon redirecting to a new URL.

The characters are:

! * _

I have an old website:

old.com/forums

and a new website:

new.com/community

I'd like to redirect many URLs, some which look like this:

old.com/forums/thread/68.baking!muffins!_yummy*

old.com/forums/thread/419.hellothere

I want them to look like this:

new.com/community/thread/68.bakingmuffinsyummy

new.com/community/thread/419.hellothere

Here's my failed attempt at a directive to remove the ! character:

rewrite ^/forums(.*)[!](.*)$   new.com/community$1$2 permanent;

It only removes the last !, and doesn't work if there isn't a ! present. What am I doing wrong? Is this even possible with regex?

Trevor Hateley
  • 451
  • 4
  • 6
  • 1
    Show how new url should looks like – Alexey Ten Jun 07 '18 at 08:49
  • 1
    You can rewrite the characters recursively and then redirect the final result. See [this answer](https://serverfault.com/questions/888059/finish-processing-rewrite-directives-in-location-and-return-301/888098#888098) – Richard Smith Jun 07 '18 at 09:02
  • thanks @RichardSmith here's what I came up with: `location /forums/ { rewrite ^(.*)\!(.*)$ $1$2 last; rewrite ^(.*)\*(.*)$ $1$2 last; rewrite ^(.*)\_(.*)$ $1$2 last; rewrite ^/forums(.*)$ community$1 permanent; }` I'm sure it could be cleaner, but it's working! – Trevor Hateley Jun 07 '18 at 20:40
  • 1
    You could probably combine the first three regular expressions into `^(.*)[!*_](.*)$` – Richard Smith Jun 07 '18 at 21:42

0 Answers0