regexurl-rewritingisapi-rewrite

Rewriting a URL to remove certain characters


I need to rewrite some blog URLs to remove certain characters. These are the along the lines of "a556" (a is always present, the numbers are always 3 digits and are random). This is proceeded by either a single or double hyphen, which I also need to remove.

These need to redirect from:

[domain]/blog/[article_name]-a556

or

[domain]/blog/[article_name]--a556

To

[domain]/blog/[article_name_with_characters_removed]

I think the regex to detect the text to be removed is:

([-]{1,2}a[0-9])\w+

But I don't know how to put this into a Rewrite rule.

Can anyone help?


Solution

  • Are you looking for a function to process your old URLs into new ones? Something like this should do the trick, if you have an array of URLs:

    var processedURLs = oldURLs.map(function(url) {
      return url.replace(/[-]{1,2}a[0-9]+/, '');
    })