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?

3 Likes

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!

2 Likes

We’re coming up on the 1-year anniversary of this, let’s celebrate this together, @perry :smile: Any updates? :crossed_fingers:

hi there mentalizer,

sadly i see no movement on the issue. I’ve commented that we are still getting asks for this, and also looped in the product team and provided the feedback i saw surfaced in this thread. hopefully someday. if others are follow along, please comment on this feature request and let us know how having regex would improve our products for you - the more compelling use cases we have, the more likely we are to make this happen.

1 Like

Has there been any movement on this feature request? We’re moving a bunch of sites to Netlify and are really going to miss this functionality if it’s not available.

You can use Edge Functions to achieve this functionality. Regex in redirects is not supported and there are no plans to do that at the moment.