Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Long story short, I want to have both and working together. I'm using apache and not very good at writing regex.

I have already a global RewriteRule which sends everything to the app entry point. What I need is to tell apache if length($path) is <= 5 chars then rewrite to another location.

I know I can use {1,5} like syntax in regex, but don't really know if it's what I'm looking for.

I'd like to implement this at web-server level rather than php level. Any help is appreciated.

share|improve this question
up vote 2 down vote accepted

If you can assume that the - character will not be included in short URLs:

RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]){1,5} 
RewriteRule ^ /app-entry-point?url=%1 [L]

This is untested but something like this should work. Essentially you're passing a condition if REQUEST_URI is something like /abc12, control switches to whatever handler you want. It will skip URLs over 5 characters or that include the 'slug' character "-" or an underscore.

%1, the last RewriteCond backreference, will be expanded to the short URL. Put this above your other rules to enforce this first -- with the [L] flag it will stop rewriting URLs and pass control to the app.

share|improve this answer
Awesome thanks! – Renato Renato Dec 27 '10 at 16:38
 RewriteCond %{REQUEST_URI}   ^/.{1,5}$
 RewriteRule ...

This will match any request between one and five characters in length, and will run the following RewriteRule. Anything longer then that will not run the RewriteRule. The REQUEST_URI variable is what the browser passed to the web-server. The same could be handled in a single RewriteRule, but the above allows chaining if you need it.


^ = Matches the start of the string
/ = Just matches a forward slash
. = Matches any one character that isn't a newline
{1,5} = Informs the regex engine to match the previous character 1-5 times
$ = Matches the end-of-line character

The /.{1,5}$ construct tells the regex engine to match any string of size 1-5 that is immediately followed by the end-of-line character and immediately preceded by the / character.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.