Rewrite a Shortened URL to a Pretty URL?

Javi Lavandeira javi at
Wed Aug 22 08:52:02 UTC 2012

On 2012/08/22, at 15:23, "slevytam" <nginx-forum at> wrote:

> Currently, I use a basic rewrite for my url shortener.
> if (!-e $request_filename) {
>     rewrite ^/(.*)$ /entry/index.php?id=$1 permanent;
> }
> So I would like following scenario:
> to rewrite to
> while showing the user
> in the address bar

Just change your regexp to match everything up to the first slash in the URL and ignore the rest. From memory, this would be something like this:

rewrite ^/(.*?)/.*$ /entry/index.php?id=$1 permanent;

If this regexp syntax is correct (please check it, I'm replying from the subway and can't check the manpages), then this should select everything in the URL up to the first slash, assign it to the $1 positional parameter, and ignore the rest. 

The idea is that if your pretty URL is

then the regexp would match the "1234" regardless of what's after it. GoogleBot will be happy and you'll get more visitors. 


Javi Lavandeira

Twitter: @javilm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list