How to use regex in redirects

Is it possible to use regular expressions when setting up redirects?

I have a bunch of URLs that originally had underscores in them, but now have dashes instead (something something SEO). Unfortunately, they are still linked to from around the net using the underscore URLs and I would like to redirect them to their proper dasherized versions instead of serving 404 pages.

For example:

There are a large number of other URLs following the same pattern and I am far too lazy to generate a line with a redirect for each.

In the olden days of Apache I had a .htaccess with

RewriteRule ^(journal/.*?/[^/]*?)_(.*_.*)$  /$1-$2 [N]
RewriteRule ^(journal/.*?/[^/]*?)_(.*)$     /$1-$2 [R=301]

which did the trick. Something similar in _redirects would be perfectly fine for me, but it doesn’t seem like it’s supported.

What are my options? Could it be handled by an Edge Handler if I were to write one?

1 Like

Hi, @mentalizer. You are correct that redirects to not support regular expressions at this time.

Regarding if this could be done using an Edge Handler, yes, I do believe that this functionality could be achieved with that method.

That’s too bad, it would be a nice, generic addition I think :o/

Oh well, I’ve signed up for Edge Handlers to see if they might do the trick…

This would be a great to post on the feature requests thread :slightly_smiling_face:

1 Like

I too was hoping to leverage a regex in _redirects for the same exact purpose (rewriting underscores in blog post links to hyphens); sadly, my pre-Web file naming convention has not withstood the test of time.

Surprisingly, however, the Google developer documentation style guide has a rather nuanced view on underscores in filenames:

Make file and directory names lowercase. In general, separate words with hyphens, not underscores. Use only standard ASCII alphanumeric characters in file and directory names.

Exceptions for consistency

If you’re adding to a directory where everything else already uses underscores, and it’s not feasible to change everything to hyphens, it’s okay to use underscores to stay consistent.

Recommended: avoiding-cliches.jd
Sometimes OK: avoiding_cliches.jd

Would still be awesome if _redirects could support regexes, but underscores do not appear to be a total disaster from Google’s point of view.

hi @miles ,

i had a chance to confirm today that we don’t, at this time, support regex in redirects. I can file a feature request on this, but i can’t say when we might work on this unfortunately :frowning:

Thank you, Perry; I deeply appreciate your response and willingness to file a feature request.

The request has already been tracked as part of the generic “Expanding functionality of redirects” request: Expanding functionality of redirects.

A more specific feature request might be a good idea for increased visibility, I guess. I’d appreciate you filing that as well :slight_smile:

hi there,

I have filed a feature request - if i get more information on work that goes into that, I’ll update you here!