RegEx for Redirect Rules and SSL

At times you may need to match several various URLs based on a set of rules. If you need to create 301 redirect rules or specify pages to apply your SSL, for example. To efficiently define a group URLs you’ll will need some knowledge of RegEx.


About RegEx

A “Regular Expression”, or RegEx, is a set of characters that create a search pattern. It’s useful for redirect and SSL rules because it allows us to dynamically match any number of URLs without creating a rule for each and even without knowing what those URLs might all be.


RegEx Basics

The RegEx basic characters and phrases are useful to have an understanding of because you may need to modify our default examples later on in this article.

Character

Interpretation

^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 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 & 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”.

AlphanumericRegEx
image.pngimage\.png

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

AlphanumericRegEx
some phrasesome\ phrase


RegEx for Redirects

Information on adding redirects to your User Portal can be found here.

Primary Fields

  • Source — Your RegEx will go here
  • Destination — Full destination URL including HTTP or HTTPS
  • Advanced settings > Match args — (Optional) Argument and value to append to the destination

WP Engine Redirect Rule Conditions

  • Redirects should be added in the User Portal versus the .htaccess or with a plugin, if possible.
    • The User Portal redirects will be faster and work more consistently.
    • Redirects in the .htaccess or through a plugin can work, but they are subject to caching.
    • Only Redirects set in the User Portal will work for static assets, such as png and html files.
  • Redirects are read top to bottom.
    • More specific rules should be added first.
  • Rules are case-sensitive.
    • Case-insensitive RegEx may be necessary.
  • Argument values cannot include RegEx.
  • ASP and ASPX pages can only be redirected from the User Portal.
    • Attempting to redirect these pages with a plugin or the .htaccess will not work on WP Engine.


Redirect Examples

InterpretationSource RegExDestination
Redirect from page1 to page2^/page1/?$https://example.com/page2/?
Redirect a path, but not the page^/old/page/?$https://example.com/new/page/?$
Redirect anything in a path to a new path, changing the page^/old/(.*)https://example.com/new/$1
Redirect anything in path to a new path, but not changing the page^/old/(.+)https://example.com/new/$1
Wildcard redirect to change the second directory and the page^/old/(.+)/diff/(.+)https://example.com/new/$1/other/$2
Redirect a static asset, such as html^/old/page\.htmlhttps://example.com/new/page.html
Redirect all pages to a subdirectory, and append the page to the URL using a capture group^/(.*)?$https://example.com/somedirectory/$1

Case-Insensitive Redirects

Most URLs by default are lowercase. Case-insensitive redirects allow you to redirect a URL that you know may be requested with some letters in the upper or lower cases.
If redirecting to the lowercase version of the phrase a $ at the end of the source with no trailing slash, and a trailing slash on the end of the destination should prevent a redirect loop.

InterpretationSource RegExDestination
Upper/lower case to new^(?i)/post$https://example.com/newpost/
Upper/lower case to lower case^(?i)/post$https://example.com/post/?

Redirects with Query Args

An “arg” is the same a URL query. In a URL you can spot the Arg or Query by a question mark ?.

As a ? is a reserved RegEx character this means args need to be redirected a certain way.

  • It is assumed that ? is already present when placing the value information in the Advanced Settings > Match Args field. Do not add a ? in the Match Args field to denote an arg is present.
  • The Match Args field does not support RegEx.
    • This means no wildcard arg redirects. You will need to use a more broad wildcard that does not require the use of an arg.
  • The query and value are automatically appended as-is to the destination or excluded from the destination completely.
  • Appending a ? to the end of the destination prevents the query and value from being appended to the destination.
InterpretationSource RegExMatch ArgsDestination
Keep the arg appended^/old-pagearg=somethinghttps://example.com/new-page/
Do not preserve the arg^/old-pagearg=somethinghttps://example.com/new-page/?

Negative Lookahead

A negative lookahead allows you to include anything except the denoted value.

Source RegExInterpretation
^/(?!(page)/?$)Match all URLs, except for those ending with the specified phrase
^/(?!(page1|page2|page3)/?$)Match all URLs except for those ending with multiple specified phrase
^(?<!woff2)$Match all URLs except for those ending with woff2
^/(?!blog(/|$))Match all URLs except for ones starting with /blog
^((?!some-phrase).)*$Match all URLs except for those containing “some-phrase”

SSL Redirects

If you do not wish to force your SSL to “Secure All URLs” then you will likely need to specify certain URLs to secure instead. In order to configure your site to use the SSL correctly in this case, you will need to configure URL paths with regex.

InterpretationSource RegEx
Secure a directory, but nothing below it^/shop/?$
Secure all directories below “shop”^/shop/.*
Secure a single file^/shop.php
Secure any file of a specific type^/shop/.*.php
Secure anything that contains the phrase “shop”^/.*shop
Match “shop” anywhere in the URL regardless of upper or lower case^/(?i)shop

RegEx Tools and Resources

Regex Pal
Regex Syntax Breakdown
Regular Expressions Info
RegEx 101


NEXT STEP: How to add or import redirect rules

Still need help? Contact support!

We offer support 24 hours a day, 7 days a week, 365 days a year. Log in to your account to get expert one-on-one help.

The best in WordPress hosting.

See why more customers prefer WP Engine over the competition.