Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
Web Dev + WordPress + Security

Redirecting URLs that Include Numbers

Redirecting stuff with .htaccess generally is pretty straightforward, but there can be a lot of confusion when it comes to targeting patterns that include numbers. I think this largely is due to the syntax used for matching numbers in regular expressions. It’s sort of unintuitive until you get the hang of it. So to help in that regard, this tutorial explains the basics of matching numbers with .htaccess, and then provides some useful examples that should get you there.

Basics of regex number matching

Without going into the logic and mechanics of numerical regular expressions, here is the basic idea:

[0-9]*       matches zero or more of any number       e.g., 
[0-9]+       matches one or more of any number        e.g., 123
[0-9]{8}     matches any 8 numbers                    e.g., 12345678
[0-9]{8,}    matches any 8 or more numbers            e.g., 1234567890
[0-9]{0,8}   matches any number 0-8 times             e.g., 123456
[0-9]{4,8}   matches any number 4-8 times             e.g., 1234

[5]          matches the number 5 one time            e.g., 5
[5]+         matches the number 5 one or more times   e.g., 555
[5-9]{2}     matches any numbers 5-9 two times        e.g., 56
[5-9]{0,4}   matches any numbers 5-9 four times       e.g., 5678
[5-9]{4,8}   matches any numbers 5-9 4-8 times        e.g., 56789567

To test any of these (or other) regular expressions, you can use a good regex testing tool. These examples cover just about any possible number(s) and/or numerical ranges. Equipped with this information, let’s use Apache’s mod_alias to redirect some URLs that include numbers.

Redirect numbers via mod_alias

Apache’s alias module provides two great directives for redirecting URLs: Redirect and RedirectMatch. Here are some examples that demonstrate how to redirect URLs that include numbers.

Example 1

Say we want to redirect any URL that contains 32 or more numbers. We can do this with RedirectMatch, like so:

<IfModule mod_alias.c>
	RedirectMatch 301 [0-9]{32,} /wherever/index.html

Likewise, if we want to block any request that contains 64 or more alphanumeric characters:

<IfModule mod_alias.c>
	RedirectMatch 403 [a-zA-Z0-9]{64,}

By replacing the regex patterns used in these examples, it’s possible to redirect just about anything to anywhere.

Example 2

Say we have the following resources that we would like to redirect:


For these resources, we want to prepend a site identifier, s123-, like so:


To do this, we can use RedirectMatch:

<IfModule mod_alias.c>
	RedirectMatch 301 /([0-9]{5})-([0-9]{10})\.(gif|jpg|png)$ /s123-$1-$2.$3

In English, this says:

  1. Match any number exactly five times,
  2. followed by a hyphen,
  3. followed by any number matched exactly ten times,
  4. followed by a literal dot,
  5. followed by one the specified image types.

Then for any matching URLs, redirect the request to the following location:


..where 89859, 1326045863, and png are the same as in the original URL.

Redirect numbers via mod_rewrite

While redirecting with mod_alias is simple and works great, we can get more flexibility by using Apache’s mod_rewrite. Here are some examples to demonstrate how it’s done.

Example 1

In this example, we use mod_rewrite to redirect all URLs that look like these:



That is, we want to redirect any of the following:

  • Any request that begins with /temp/ followed by exactly 9 numbers
  • Any request that begins with /cache/ followed by 5-32 alphanumeric characters

Here is one way to achieve it:

<IfModule mod_rewrite.c>
	RewriteCond %{REQUEST_URI} /temp/([0-9]{9})\.php [NC,OR]
	RewriteCond %{REQUEST_URI} /cache/([a-z0-9]{5,32})\.php [NC]
	RewriteRule (.*) /wherever/index.html [L,R=302]

A few points about this technique:

  • Important: the [OR] flag is excluded on the last RewriteCond
  • To change the location to which matched URLs will be redirected, edit /wherever/index.html
  • As-is, a 302 “Temporary” status code will be sent along with the redirect; you can change that to any valid status code

Let’s check out one more example before wrapping up..

Example 2

This example uses mod_rewrite to match the following URLs:



That is, we want to redirect any of the following:

  • Any query string that includes key followed by exactly 10 alphanumeric characters
  • Any query string that includes hash followed by 3-20 numbers

Here is the magic code:

<IfModule mod_rewrite.c>
	RewriteCond %{QUERY_STRING} key=([a-z0-9]{10}) [NC,OR]
	RewriteCond %{QUERY_STRING} hash=([0-9]{3,20}) [NC]
	RewriteRule (.*) - [F]

A couple of points about this technique:

  • Important: the [OR] flag is excluded on the last RewriteCond
  • As-is, any matched requests are forbidden via 403 server response, customize as needed

Related resources

I hope this article helps with your redirecting needs. For more information on redirecting with Apache/.htaccess, check out the following resources:

As always, questions and comments welcome :)

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
.htaccess made easy: Improve site performance and security.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Deep dive into WP theme development.
Air finally clearing here in WA. Feeling grateful to breathe again. #oxygenmatters
Past week here in WA state has been hellish. So much smoke, like living in a chimney.
Now in September, I’m where I wanted to be in March.
Spent some time updating my article on unsafe characters, once again current with latest IETF specification.
Just realized that “Neo” is an anagram for “One”. As in, “he is the One” (The Matrix).
To get VLC app to load all songs (including subfolders), go to Preferences ▸ Show All ▸ Playlist ▸ Subdirectory behavior ▸ Expand.
Switching from PhotoShop to Affinity Photo is one of the most liberating work-related things I've done in 20 years.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.