1

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?

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
Cropsy
  • 31
  • 2
  • You want to *redirect* those old URLs to new URLs? Or you want to generally make your URLs appear in that new format? – deceze Jun 14 '16 at 09:48
  • Tip: These kinds of vanity URLs are easy to exploit, as malicious users can forge links that look like `[domain]/blog/something-really-nasty-and-offensive-a556`. Better you check the vanity part as well, instead of throwing it away. – Tomalak Jun 14 '16 at 09:53
  • @Tomalak What would somebody gain from that...? – deceze Jun 14 '16 at 10:09
  • Correct, I was going to redirect, but I think otajor's answer below should solve the problem. Thanks for the responses, though, much appreciated. – Cropsy Jun 14 '16 at 10:25
  • @deceze Well, what do trolls gain from their activities? I remember a case quite a while ago that generated some outrage, so I put unchecked vanity URLs on my mental "don't"-list, that's all. – Tomalak Jun 14 '16 at 10:52

3 Answers3

0

Please try this:

RewriteEngine On
RewriteRule (.*)-{1,2}a\d{3}(.*) $1$2 [R]
Yaroslav
  • 1,241
  • 10
  • 13
-1

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]+/, '');
})
otajor
  • 953
  • 1
  • 7
  • 20
-1

This rewrite rule could does the trick:

blog/(.+[a-zA-Z0-9])-+a[0-9]+      blog/$1

You can simplify [a-zA-Z0-9] removing all characters ranges that can't appear in the end of the articles name slug (ie [a-z0-9] or [a-z]).

JMmak
  • 1
  • 1