# How to redirect internally?

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-internally/221729>\
**Category:** NGINX\
**Created:** [July 27, 2012, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729 "2012-07-27T14:12:49Z")\
**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:** [July 27, 2012, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729/1 "2012-07-27T14:12:49Z")

</div>

Hi,

I have created a config so that a URL like  
[www.mysite.com/abc/electronics.html](http://www.mysite.com/abc/electronics.html) is rewritten as  
[www.mysite.com/index.php/electronics.html?store=abc](http://www.mysite.com/index.php/electronics.html?store=abc).

This is what I see in Mozilla Firebug

GET abc/electronics.html

301 Moved Permanently

GET index.php/electronics.html?store=abc

200 OK

What is annoying is that the browser now shows the rewritten URL and not  
the pretty URL. How can we ensure that the redirection happens only  
internally?

Thanks,  
Yash

---

<div class="post-metadata">

**Author:** ![Yash\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yash\_G](https://www.ruby-forum.com/u/Yash_G)\
**Post date:** [July 27, 2012, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729/2 "2012-07-27T14:22:46Z")

</div>

On 27 July 2012 13:12, Yash G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> GET index.php/electronics.html?store=abc  
> 200 OK
> 
> What is annoying is that the browser now shows the rewritten URL and not the  
> pretty URL. How can we ensure that the redirection happens only internally?

If you’re rewriting from/to inside the same server{} stanza, you can  
investigate using the “last” flag in the rewrite documentation:

> **[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)**
>
> The ngx\_http\_rewrite\_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

I don’t know how you can achieve this across different server{}s (with  
the obvious proviso that you can have more than one server\_name in a  
server{}).

## HTH, Jonathan

Jonathan M.  
Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![Yash\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yash\_G](https://www.ruby-forum.com/u/Yash_G)\
**Post date:** [July 27, 2012, 2:33pm UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729/3 "2012-07-27T14:33:52Z")

</div>

On Fri, Jul 27, 2012 at 05:42:17PM +0530, Yash G. wrote:

Hi there,

> I have created a config so that a URL like  
> [www.mysite.com/abc/electronics.html](http://www.mysite.com/abc/electronics.html) is rewritten as  
> [www.mysite.com/index.php/electronics.html?store=abc](http://www.mysite.com/index.php/electronics.html?store=abc).

> What is annoying is that the browser now shows the rewritten URL and not  
> the pretty URL. How can we ensure that the redirection happens only  
> internally?

> **[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)**
>
> The ngx\_http\_rewrite\_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

rewrites are internal, unless you do something to turn them into  
redirects  
(= external).

What is the rewrite directive you are using? Are are you doing anything  
the documentation says makes it become a redirect?

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Yash\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yash\_G](https://www.ruby-forum.com/u/Yash_G)\
**Post date:** [July 29, 2012, 12:52pm UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729/4 "2012-07-29T12:52:41Z")

</div>

I suggest you show your wider config and some verbose curl -v output  
against nginx.

On 29 July 2012 05:02, Yash G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > [www.mysite.com/index.php/electronics.html?store=abc](http://www.mysite.com/index.php/electronics.html?store=abc).  
> > > What is the rewrite directive you are using? Are are you doing anything
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Jonathan M.  
Oxford, London, UK

> **[Jonathan Matthews :: Words Typed, Great Rates For Cash](https://www.jpluscplusm.com)**

---

<div class="post-metadata">

**Author:** ![Yash\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7ab992/32.png) [@Yash\_G](https://www.ruby-forum.com/u/Yash_G)\
**Post date:** [July 29, 2012, 6:03am UTC](https://www.ruby-forum.com/t/how-to-redirect-internally/221729/5 "2012-07-29T06:03:11Z")

</div>

All the rewrites are marked as last. e.g.  
rewrite ^/([a-zA-Z]+)(.\*) $2?store=$1 last;
