# Rewrite a Shortened URL to a Pretty URL?

**URL:** <https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400>\
**Category:** NGINX\
**Created:** [August 27, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400 "2012-08-27T14:01:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 27, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400/1 "2012-08-27T14:01:59Z")

</div>

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](http://www.domain.com/1234) to  
[http://www.domain.com/entry/index.php?id=1234](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](http://www.domain.com/1234) to rewrite to  
[http://www.domain.com/entry/index.php?id=1234](http://www.domain.com/entry/index.php?id=1234) while showing the user  
[http://www.domain.com/1234/this-is-the-pretty-part](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:

> **[Rewrite a Shortened URL to a Pretty URL?](https://forum.nginx.org/read.php?2,229974,229974#msg-229974)**
>
> 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

---

<div class="post-metadata">

**Author:** ![slevytam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@slevytam](https://www.ruby-forum.com/u/slevytam)\
**Post date:** [August 27, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400/2 "2012-08-27T14:04:27Z")

</div>

On Wed, Aug 22, 2012 at 1:23 PM, slevytam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This forwards a url like [http://www.domain.com/1234](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)

---

<div class="post-metadata">

**Author:** ![slevytam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@slevytam](https://www.ruby-forum.com/u/slevytam)\
**Post date:** [August 27, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400/3 "2012-08-27T14:04:29Z")

</div>

On 2012/08/22, at 15:23, “slevytam” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.domain.com/1234) to rewrite to  
> [http://www.domain.com/entry/index.php?id=1234](http://www.domain.com/entry/index.php?id=1234) while showing the user  
> [http://www.domain.com/1234/this-is-the-pretty-part](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](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: [lavandeira.net](http://www.lavandeira.net/blog)

---

<div class="post-metadata">

**Author:** ![slevytam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@slevytam](https://www.ruby-forum.com/u/slevytam)\
**Post date:** [September 1, 2012, 7:52pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400/4 "2012-09-01T19:52:58Z")

</div>

On Sun, Sep 2, 2012 at 12:46 AM, slevytam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> work for [http://www.domain.com/1234](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](http://www.domain.com/1234/pretty-url-goes-here) it changes the address bar  
> to [http://www.domain.com/entry/id=1234](http://www.domain.com/entry/id=1234) instead of staying at the pretty  
> url.
> 
> Any thoughts?

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

---

<div class="post-metadata">

**Author:** ![slevytam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@slevytam](https://www.ruby-forum.com/u/slevytam)\
**Post date:** [September 1, 2012, 7:47pm UTC](https://www.ruby-forum.com/t/rewrite-a-shortened-url-to-a-pretty-url/222400/5 "2012-09-01T19:47:08Z")

</div>

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](http://www.domain.com/1234/pretty-url-goes-here) but it does  
not  
work for [http://www.domain.com/1234](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](http://www.domain.com/1234/pretty-url-goes-here) it changes the address  
bar  
to [http://www.domain.com/entry/id=1234](http://www.domain.com/entry/id=1234) instead of staying at the pretty  
url.

Any thoughts?

Thanks,

slevytam

Posted at Nginx Forum:

> **[Re: Rewrite a Shortened URL to a Pretty URL?](https://forum.nginx.org/read.php?2,229974,230447#msg-230447)**
>
> 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
