Cloud Insidr

Cybersecurity in the Age of the Machine

  • Subscribe!
  • Privacy Policy
  • Legal
  • Contact Us

Join us on Twitter: @CloudInsidr

  • news & alerts
    • events
    • industry analysis
    • industry gossip
    • people
  • cloud, edge & co.
    • AWS
    • administration & orchestration
      • web servers in the cloud
      • mail servers
      • databases
  • cybersec & warfare
    • encryption
  • blockchain
Home Uncategorized RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites
RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites

Anna E Kobylinska 2016-02-04 5 Comments

RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites

There are two major no-nos when it comes to writing NGINX redirects:

  • thou shalt not use if statements (that’s a major no-no);
  • thought shalt not use rewrites (they are a waste of resources, as in “slow” and “expensive”).

Instead, use ol’ good location blocks. But what if you have plenty of URIs to redirect? How do you deploy RegEx magic with NGINX? Easy.

For example, in order to eliminate the year, month and day from an URL, use this (updated!) regular expression:

location ~ /magazine/(\d\d\d\d)/((\d\d)\/)+(.*){ 
return 301 http://$server_name/magazine/$4; 
try_files $uri $uri/ /index.php?$args; 
}

The first capture group (\d\d\d\d) represents a four-digit year, the second and third are nested and capture the month and the day (if available), followed by a forward slash and zero or more occurrences of any characters (.*). The return directive will output only the fourth capture group [$4] that is defined as any number of characters other than the line break (.+). The important thing to note is the fact that NGINX does not insist that you mask forward slashes (/) for reasons of simplicity (except when used in a capture group between brackets), yet it still counts capture groups the way you would expect it to.

In case of doubt, you may want to refer to an online regex editor such as regexr.com  or regex101.com. For those who need to refresh their regex memory, here is a nice cheat sheet for regular expressions.

Restart NGINX to apply configuration changes. (So far so good!)

But what do you do to ensure that you haven’t overlooked some God-forsaken URLs that will deliver nasty 404 errors until the cows come home? Easy: check the errors in Google Webmaster Tools (and also, read your email: should you mess up in a major way, that’s where Google will complain, just before penalizing your site). Even if crawling failed only intermittently, it is still advisable to mark Google’s complaints as resolved once they have been resolved, obviously.

Resolving pesky 404 errors in Google Webmaster Tools
Resolving pesky 404 errors in Google Webmaster Tools

To verify if your 301 redirects are working, check out this post: SEO Secrets: How to Verify that Google Respects Your 301 (and 302) Redirects, Ranking-Wise.

Filed Under: Uncategorized, web servers in the cloud Tagged With: NGINX, RegEx, regular expressions

Comments

  1. Ariel Franks says

    2019-04-30 at 1:30 am

    I read this piece about URL redirects in NGINX using regex without rewrites! Thanks! It’s a remarkable article.

    Reply
  2. Rosario Earl says

    2019-04-29 at 2:59 am

    Hey there! I just want to offer you a huge thumbs up for your excellent information you have right here on this post.
    I will be returning to your site for more soon.

    Reply
  3. Alberta says

    2019-04-28 at 3:06 am

    Great info. Lucky mee I ran across your blog by accident
    (stumbleupon). I’ve bookmarked it for later!

    Reply
  4. Lyda says

    2019-04-14 at 9:12 am

    You’re so interesting! I do not suppose I’ve truly read a single thing like that
    before. So nice to find another person with some genuine thoughtfs on thos
    subject matter. Really.. many thanks for starting this up.
    This website is onee thing that is required on the web, someone with
    some originality!

    Reply

Trackbacks

  1. Redirect a Subdirectory in NGINX - Cloud Insidr says:
    2022-03-21 at 8:25 am

    […] For more NGINX redirects see this post: RegEx Bliss: Set Up URL Redirects in NGINX Using Regular Expressions without Rewrites. […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe

Find the monitor that's right for you.

SSL/TLS Certificate Square (250 x 250)

Tag Cloud

AWS AWS Billing Dashboard AWS Config AWS Cost Explorer AWS EBS AWS Resource Explorer AWS Tag Editor AWS Trusted Advisor cipher suites cyber security Diffie-Hellman EBS EC2 ECDHE Five Eyes Alliance FLAME HSTS HTTP/2 Java Linux Log4j NGINX NVMe PHP RCE Route 53 SELinux SHA-1 SHA-2 SQL SSL TLS

Pearson Education (InformIT)

SSL/TLS Certificate Medium Rectangle (300 x 250)

©2022 CybrAnalytiqa OÜ

  • Content purchasing and syndication