URI re-mapping using try_files

Francis Daly francis at daoine.org
Thu Oct 11 20:32:20 UTC 2012

On Thu, Oct 11, 2012 at 03:59:24PM -0400, mrtn wrote:

Hi there,

> Thanks for introducing me to rewrite directive. Just to confirm, this is how
> I should use your rewrite:
> root     /home/www/example;
> location /public/doc/ {
>     rewrite           (.*)/(.*) $1/$2/$2.html break;
> }

It can work outside of all locations, or else in the one location that
handles this request.

So this config can work.

> Ideally, for the other cases you raised, I want the following to happen:
> >http://www.example.com/public/doc/abc123
> >http://www.example.com/public/doc/abc123?para=nodata
> when the query string (e.g. ?para=blah) part is missing or incomplete, I
> want to serve a generic error page (e.g. /error.html)

The above rewrite pays no attention to query strings.

So you'll want to do something based on $arg_para -- maybe an "if"
or something involving "map".

I guess (without testing):

    if ($arg_para != data) { return 404; }

inside that location{} would probably work.

> >http://www.example.com/public/doc/abc123/abc123.html
> when the user tries to access the actual html page directly, I want to block
> it by either returning a 404 or serving a generic error page as above

The above rewrite does that (assuming that the "rewritten" file is absent).

> >http://www.example.com/public/doc/one/two
> when the user queries an URI that has no corresponding .html file on the
> server, I want to simply return a 404.

The above rewrite does that; but which html file should it look for here?

/home/www/example/public/doc/one/two/two.html, or

(As in: do you repeat everything after /public/doc, or do you repeat
just the final after-slash part?)

> Can all these be implemented using rewrite only? Thanks.

With the extra "if" above, I think so.

Your testing should be able to show any problems, or unexpected behaviour.

Francis Daly        francis at daoine.org

More information about the nginx mailing list