Rewrite assistance

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:

On 23 Nov 2010 21h41 WET, [email protected] 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

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

Module ngx_http_rewrite_module 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. [email protected]

Francis D. Wrote:

has an example.

Good luck,

f

Francis D. [email protected]

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:

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:

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. [email protected]

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.
http://sysoev.ru/en/