# Rewrite assistance

**URL:** <https://www.ruby-forum.com/t/rewrite-assistance/199548>\
**Category:** NGINX\
**Created:** [November 23, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548 "2010-11-23T22:42:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [November 23, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/1 "2010-11-23T22:42:16Z")

</div>

Hi all,

I’m trying to add in a rewrite rule that will take this string and just  
do a redirect to /  
/preview.php?controllerName=channel&id=2

Since the query string can vary, how would I craft my rewrite  
appropriately to do this? I’ve tried a few variations of the following,  
but all it does is remove the “preview.php” portion of the string,  
leaving the “?controllerName=channel&id=2”  
rewrite ^/preview.php(.\*) / permanent;

Essentially I just watch to match against “preview.php” and redirect to  
the index portion of the site.

Thanks!

Posted at Nginx Forum:

> **[rewrite assistance](https://forum.nginx.org/read.php?2,152792,152792#msg-152792)**
>
> Hi all, I'm trying to add in a rewrite rule that will take this string and just do a redirect to / /preview.php?controllerName=channel&id=2 Since the query string can vary, how would I craft my rewrite appropriately to do this? I've tried a few...

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 23, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/2 "2010-11-23T22:54:49Z")

</div>

On 23 Nov 2010 21h41 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I’m trying to add in a rewrite rule that will take this string and  
> just do a redirect to / /preview.php?controllerName=channel&id=2
> 
> Since the query string can vary, how would I craft my rewrite  
> appropriately to do this? I’ve tried a few variations of the  
> following, but all it does is remove the “preview.php” portion of  
> the string, leaving the “?controllerName=channel&id=2” rewrite  
> ^/preview.php(.\*) / permanent;

Note that your saying with your regex that I want a numeric capturing  
group that can have _0_ characters.

Try this:

^/preview.php(?:.\*)$ / permanent;

Now you’re saying match anything with a non-capturing group to the end  
of the line.

— appa

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 24, 2010, 1:29am UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/3 "2010-11-24T01:29:59Z")

</div>

On Tue, Nov 23, 2010 at 04:41:04PM -0500, daveyfx wrote:

Hi there,

> I’m trying to add in a rewrite rule that will take this string and just  
> do a redirect to /  
> /preview.php?controllerName=channel&id=2

[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule) is worth reading.

> Since the query string can vary, how would I craft my rewrite  
> appropriately to do this? I’ve tried a few variations of the following,  
> but all it does is remove the “preview.php” portion of the string,  
> leaving the “?controllerName=channel&id=2”  
> rewrite ^/preview.php(.\*) / permanent;

rewrite appends args unless the last character of the replacement is  
“?”.

> Essentially I just watch to match against “preview.php” and redirect to  
> the index portion of the site.

This description is not the same at the earlier ones.

Assuming that this sentence describes your wish:

location = /preview.php {  
rewrite ^ /? permanent;  
}

(untested!) should probably Just Work.

If you _do_ want to vary based on the query string (?-bit), the manual  
has an example.

Good luck,

## f

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

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 24, 2010, 5:43am UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/4 "2010-11-24T05:43:36Z")

</div>

## Francis D. Wrote:

> has an example.
> 
> Good luck,
> 
> ## f
> 
> Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Francis,

Spot on. That seems to have done the trick. Out of my own desire to  
learn, is there a way to do the same thing without using the location  
directive, and just merely using a rewrite statement?

Thanks!  
Dave

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,152792,152854#msg-152854](http://forum.nginx.org/read.php?2,152792,152854#msg-152854)

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 24, 2010, 1:23am UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/5 "2010-11-24T01:23:43Z")

</div>

Antonio,

That did not work. I’m still getting the same end result where  
/preview.php?controllerName=channel&id=2 is rewritten as  
/?controllerName=channel&id=2

Essentially I just want to check to see if preview.php is the requested  
file, and if it is, redirect to / regardless of the query string  
appended after preview.php

Thanks,  
Dave

Posted at Nginx Forum:

> **[Re: rewrite assistance](https://forum.nginx.org/read.php?2,152792,152828#msg-152828)**
>
> Hi all, I'm trying to add in a rewrite rule that will take this string and just do a redirect to / /preview.php?controllerName=channel&id=2 Since the query string can vary, how would I craft my rewrite appropriately to do this? I've tried a few...

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 24, 2010, 11:44am UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/6 "2010-11-24T11:44:32Z")

</div>

On Tue, Nov 23, 2010 at 11:42:38PM -0500, daveyfx wrote:

Hi there,

> > location = /preview.php {  
> > rewrite ^ /? permanent;  
> > }

> Spot on. That seems to have done the trick. Out of my own desire to  
> learn, is there a way to do the same thing without using the location  
> directive, and just merely using a rewrite statement?

Yes.

The manual says

“”"  
syntax: rewrite regex replacement flag  
default: none  
context: server, location, if  
“”"

The location{} block just says “only try this for some urls”.

Within the server{} block, it would be “try this for all urls”, and only  
urls that match the regex would be rewritten.

So: the “rewrite” line that was almost working for you, with the extra  
bit on the replacement part to suppress adding the query string, should  
be what you want.

Good luck,

## f

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

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [November 24, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/rewrite-assistance/199548/7 "2010-11-24T12:26:42Z")

</div>

On Tue, Nov 23, 2010 at 11:42:38PM -0500, daveyfx wrote:

> > 
> 
> Francis,
> 
> Spot on. That seems to have done the trick. Out of my own desire to  
> learn, is there a way to do the same thing without using the location  
> directive, and just merely using a rewrite statement?

You can place the rewrite on server level, but it’s better to use  
the location, because otherwise nginx will run regex “^/preview.php$”  
for  
every request. Testing “location = /preview.php” is faster.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**
