Rewrite rules not working

Francis Daly francis at
Sun Sep 11 13:44:36 UTC 2016

On Sun, Sep 11, 2016 at 08:12:00AM -0400, khav wrote:

Hi there,

> I am trying to make  pretty urls using rewrite rules but they are not
> working

"Pretty urls" usually means that the browser *only* sees the original
url, and the internal mangling remains hidden.

A rewrite that leads to a HTTP redirect gets the browser to change the
url that it shows.

Sometimes that is wanted; you can judge that for yourself.

> should be rewrite to 
> location = /s1/(.*)$ { You have used "=", but your pattern resembles
a regex. This location as-is will probably not be matched by any request.

> 	 rewrite ^/s1/(.*) /file/server/$1 permanent; "permanent" there means "issue
a HTTP redirect", so the browser will make a new request for

I suggest changing it to

  location ^~ /s1/ {
    rewrite ^/s1/(.*) /file/server/$1 permanent;

You can remove the "permanent" if you do not want the external redirect
to be issued; either way, you will also need a location{} which handles
the request for /file/server/video.mp4 and does the right thing.

> should be rewrite to

With a few caveats about edge cases, something like

  location ^~ /view/ {
    rewrite ^/view/(.*) /view.php?id=$1 permanent;

should probably do what you want.

Similarly, you will need a location{} to handle the /view.php request
and do the right thing; and removing "permanent" may be useful. If you do
remove "permanent", then you probably could avoid the rewrite altogether
and just "fastcgi_pass" directly, with a hardcoded SCRIPT_FILENAME and
a manually-defined QUERY_STRING.

Good luck with it,

Francis Daly        francis at

More information about the nginx mailing list