Common Regex used for Redirect Rules and SSL

Links to Popular Tools and References:

Regex Pal
Regex Syntax Breakdown

So what about the ^ and (.*) and w{3,}?$ ?

The following is common Regex Syntax that frequently is used when Writing a Redirect. Remember that the Source Input and the Match Args Field are Regex Based, the Destination however, is not. Also covered in this article are SSL related examples.

Character Interpretation
^ Match only if the following is at the beginning of the line
$ Match only if the previous is at the end of the line
? Match the previous 0 or 1 times (makes it optional)
(?i) Match the following with case-insensitive regex
. Match any single character
* Match the previous 0 or more times
+ Match the previous 1 or more times
(x)  Matches x and remembers the match to be called using $1
(?:x) Matches x but does not remember the match
x(?=y) Matches x only if it is followed by y
x(?!y) Matches x only if it is NOT followed by y
x|y Matches x or y
[xyz] Matches any character enclosed in the brackets. Note: This matches X, or Y, or Z; it doesn’t match the string “xyz”.
  “Escape” special characters. Thus ? literally matches ? not the syntax rule of “previous is option”.
d s w Matches any Digit (number), Space (space bar), or Word Character (a-z, A-Z, 0-9, or _) respectively.
D S W Matches everything but a Digit (number), Space (space bar), or Word Character (a-z, A-Z, 0-9, or _) respectively.

For more information on each of these, this page will be helpful.


Examples

Redirecting SSL Paths

In order to configure your site to use SSL correctly, you will need to configure URL paths using regex.  Below are some examples of how to construct source regex rules for different file and path types:

Purpose

Example source formatting

Include an entire directory but nothing beneath it

http://www.yourdomain.com/shop/

^/shop/?$

Include all subdirectories

http://www.yourdomain.com/shop/*

^/shop/.*

Include a single file

http://www.yourdomain.com/shop.php

^/shop.php

Include any file of a specific type

^/shop/.*.php – any php file

Include everything that contains a specific phrase

http://www.yourdomain.com/*shop*

^/.*shop

Match /SHOP, /shop, and /ShOp ^/(?i)shop

Non-SSL URL Redirects.

1. Redirect a Page

To redirect one page to another page, just fill in the fields like so:

Name: Redirect my contact page
Domain: www.domain.com
Source: ^/old-path/contact-us/?$
Destination: /new-path/contact-us/
Redirect type: 301 PermanentCopy

NOTE

  • This Redirect Rule will match a URL of http://www.domain.com/old-path/contact-us -or- http://www.domain.com/old-path/contact-us/
  • The variation is because of the Regex Syntax “/?$”
  • The Question Mark “?” makes the trailing slash optional
  • It will also only match the Source if it Starts with a “/” (note the carrot “^” ), or ends with either “s” or “/” (note the ending “$” )
  • If the WordPress install is NOT a Multisite install, it is more efficient to leave the “Domain” field blank, or set to “All Domains”. Otherwise there will need to be an extra check to ensure the correct domain is being redirected.

2. Redirect of a static html page with a backslash:

Please notice below that there is a backslash in front of the dot of the html document. Certain characters have a specific purpose in regex so they must be escaped (ignored) in order for the rewrite rule to work properly. Just throw a backslash infront of any dot of a document type (jpeg, css, js etc.).

Name: Redirect my static html page
Domain: www.domain.com
Source: ^/olddirectory/verylong/URL/goeson/mypage.html$
Destination: /mypage/
Redirect type: 301 PermanentCopy

NOTE

  • This Redirect Rule will match a URL of http://www.domain.com/olddirectory/verylong/url/goeson/mypage.html
  • Notice how the the period has been commented out with a backslash ()? If it wasn’t then this rule would also match the URL http://www.domain.com/olddirectory/verylong/url/goeson/mypagedhtml
  • In Regex “.” matches any character, “.” matches a period literally.

3. Redirect Catch All

Use case: I have a bunch of files in a certain directory but they have all moved to another directory. All of the file names are the same but they are just in a new directory. The end of each regex is like a find and replace or a copy/paste. Whatever is at the end will be carried over with the redirect.

Name: Redirect catch all
Domain: www.domain.com
Source: ^/thisiswhere/myfileswere/(.+)
Destination: /thisiswhere/myfilesmovedto/$1
Redirect type: 301 PermanentCopy

NOTE

  • “(.+)” and “(.*)” is matching “.” any character (*) 0 or more times, or (+) 1 or more times, and remembering the match. This is the most effective “wildcard” solution in Regex.
  • You can match “.*” and “.+” with out Remembering the match as well.
  • We are calling the matched content in the Source Input “(.+)” and adding it to the Destination via “$1” – the number after the $ in the destination relates to the “capture group” or set of parentheses in the source.
  • If you were matching different parts of a URL, say “^/thisiswhere/(.+)/fileswere/(.+)” Then you could call each capture group separately using “$1” and “$2” respectively
  • You may have a need to redirect everything in one domain to another. Some people go way overboard and try to do a search and replace in the database. Lets say you have a bunch of links in your database that have your old domain name. You should actually not create a redirect here or go updating your database. The fastest and easiest thing to do is make sure that both of the domain names are pointed to your WP Engine site and add the old domain as a redirect to the new domain. The instructions for doing so coincide with the “note” of this article: Going Live

4. Redirect with URL Masking

Use case: I want a URL to load the content from a separate path on the same domain, but I want the original URL to remain unchanged. This will mask the URL where the content actually lives.

Name: Redirect and mask my URL
Domain: www.domain.com
Source: ^/myurl
Destination: /realpage/?
Redirect type: breakCopy

NOTE

  • This Redirect Rule will match a URL of http://www.domain.com/myurl and display the content of http://www.domain.com/realpage.
  • Notice there is a question mark at the end of the destination path and the redirect type is set to “break”. Both settings are needed for the masking to work.
  • redirects, regex
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Password protecting a directory on the web server

Password protecting a web directory can be a useful way to manage web content on a...

How to Prevent a Directory Listing of Your Website with .htaccess

If you create a new directory (or folder) on your website, and do not put an "index.html" file in...