Rewrite a Shortened URL to a Pretty URL?

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


On 2012/08/22, at 15:23, "slevytam" <nginx-forum at nginx.us> 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:
> http://www.domain.com/1234 to rewrite to
> http://www.domain.com/entry/index.php?id=1234 while showing the user
> http://www.domain.com/1234/this-is-the-pretty-part 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

http://www.example.com/1234/whatever-goes-here

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

Regards,

--
Javi Lavandeira

Twitter: @javilm
Blog: http://www.lavandeira.net/blog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20120822/f02c5a3b/attachment.html>


More information about the nginx mailing list