Rewrite a Shortened URL to a Pretty URL?

Hi,

I was hoping someone could help me with a rewrite question.

Currently, I use a basic rewrite for my url shortener.

if (!-e $request_filename) {
rewrite ^/(.*)$ /entry/index.php?id=$1 permanent;
}

This forwards a url like http://www.domain.com/1234 to
http://www.domain.com/entry/index.php?id=1234. It works great.

The problem is that I’ve realized that pretty urls are strongly
preferred by
google.

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

Can anyone tell me what is the best way to do this?

Thanks,

slevytam

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,229974,229974#msg-229974

On Wed, Aug 22, 2012 at 1:23 PM, slevytam [email protected] wrote:

This forwards a url like http://www.domain.com/1234 to
Can anyone tell me what is the best way to do this?

remove the permanent keyword. (note that there may be better way to do
this)

On 2012/08/22, at 15:23, “slevytam” [email protected] 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

On Sun, Sep 2, 2012 at 12:46 AM, slevytam [email protected] wrote:

work for http://www.domain.com/1234.

Also, it does not leave the url as entered. So, for example, if you input
http://www.domain.com/1234/pretty-url-goes-here it changes the address bar
to http://www.domain.com/entry/id=1234 instead of staying at the pretty
url.

Any thoughts?

rewrite ^/(\d+)(/|$) /entry/index.php?id=$1;

Hi,

Thank you for your reply. Sorry for the late response but the forum
didn’t
notify me of your post.

I tried your suggestion.

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

It works for http://www.domain.com/1234/pretty-url-goes-here but it does
not
work for http://www.domain.com/1234.

Also, it does not leave the url as entered. So, for example, if you
input
http://www.domain.com/1234/pretty-url-goes-here it changes the address
bar
to http://www.domain.com/entry/id=1234 instead of staying at the pretty
url.

Any thoughts?

Thanks,

slevytam

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,229974,230447#msg-230447

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs