Welcome! Log In Create A New Profile

Advanced

Re: Rewrite a Shortened URL to a Pretty URL?

Javi Lavandeira
August 22, 2012 05:08AM
On 2012/08/22, at 15:23, "slevytam" <nginx-forum@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_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Rewrite a Shortened URL to a Pretty URL?

slevytam August 22, 2012 02:23AM

Re: Rewrite a Shortened URL to a Pretty URL?

Edho Arief August 22, 2012 02:32AM

Re: Rewrite a Shortened URL to a Pretty URL?

Javi Lavandeira August 22, 2012 05:08AM

Re: Rewrite a Shortened URL to a Pretty URL?

slevytam September 01, 2012 01:46PM

Re: Rewrite a Shortened URL to a Pretty URL?

Edho Arief September 01, 2012 01:54PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 303
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready