Menu

RegEx guide for Flywheel Redirects

Updated on October 24th, 2023

A “Regular Expression,” or RegEx, is a set of characters that create a search pattern. RegEx is useful for redirects because it allows you to dynamically match any number of URLs without creating a specific rule for each, even without knowing what those URLs might all be.

Note

This guide covers the basics of RegEx but is by no means exhaustive. For more information, we suggest checking out the following resources:

Regex Pal
Regex Syntax Breakdown
Regular Expressions Info
RegEx 101



RegEx basics

Character Definition
^ Match only if the characters following are at the beginning
$ Match only if the characters before are at the end
? The character just before this is optional
(?i) All following characters are case-insensitive
. Any 1 character
* Match the previous character 0 or more times
+ Match the previous 1 or more times
(x) Capture group. The characters inside are called by a variable later. Variables are numerical such as $1
.* A wildcard; any letter, word, or phrase
.+ Similar to a wildcard but requires at least 1 character to be present to continue
(?!) Negative lookahead, the following characters are excluded
Escape reserved character, so it functions as alphanumeric instead, goes just before each reserved character

Special and reserved characters

Any RegEx functional character is not assumed to be read as alphanumeric, but rather as its function in RegEx.

For example, this is relevant when trying to redirect static assets which would include a period before the file extension. That period must be escaped with a backslash just before the period to say “read the next character as alphanumeric instead of as RegEx”.

Alphanumeric RegEx
image.png image.png

The space character must be escaped with a backslash proceeding the space character as well.

Alphanumeric RegEx
example phrase example phrase

Redirect examples

Interpretation Source RegEx Destination
Redirect from page1 to page2 ^/page1/?$ https://example.com/page2/
Redirect only part of the path ^/old/page/?$ https://example.com/new/page/
Redirect to a new starting path, while carrying over anything that follows. Matches root page request as well as any following arguments. ^/old/(.*) https://example.com/new/$1
Redirect to a new starting path, while carrying over anything that follows. Requires a following argument. ^/old/(.+) https://example.com/new/$1
Capture and reuse multiple pieces of the previous path in new destination ^/old/(.+)/diff/(.+) https://example.com/new/$1/other/$2
Redirect a static asset, such as a PDF file ^/old/page\.pdf https://example.com/new/page.pdf
Redirect all pages to a subdirectory, and append the page to the URL using a capture group ^/(.*)?$ https://example.com/somedirectory/$1

Need help?

If you have any questions our Happiness Engineers are here to help!

Was this article helpful?

Getting Started

New to Flywheel? Start here, we've got all the information you'll need to get started and launch your first site!

View all

Account Management

Learn all about managing your Flywheel user account, Teams and integrations.

View all

Billing

Everything about billing, invoices and payments can be found here.

View all

Features

Flywheel hosting plans include a ton of great features. Learn about how to get a free SSL certificate, set up a staging site, and more!

View all

Platform Info

All the server and setting info you'll need to help you get the most out of your Flywheel hosting plan!

View all

Site Management

Tips and tricks for managing your sites on Flywheel, including going live, troubleshooting issues and migrating or cloning sites.

View all

Developer Hub

Learn how to connect, deploy, and more with SSH on Flywheel's platform.

View all

Growth Suite

Learn more about Growth Suite, our all-in-one solution for freelancers and agencies to grow more quickly and predictably.

View all

Managed Plugin Updates

Learn more about Managed Plugin Updates, and how you can keep your sites up to date, and extra safe.

View all

 

Flywheel help

Help is just a click away! Log into Flywheel dashboard to instantly chat with an expert, respond to a ticket, or follow along with in-depth documentation. We happily offer support 24 hours a day, 7 days a week, 365 days a year!

Log in

Try Flywheel today

Launch your next WordPress site in minutes.

 Free migrations  24/7/365 support  14 day demo sites