Rewrite rules not working

Francis Daly francis at daoine.org
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.

> https://example.com/s1/video.mp4 should be rewrite to 
> https://example.com/file/server/video.mp4
> 
> location = /s1/(.*)$ {

http://nginx.org/r/location. 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;

http://nginx.org/r/rewrite. "permanent" there means "issue
a HTTP redirect", so the browser will make a new request for
/file/server/video.mp4.

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.

> https://example.com/view/video5353 should be rewrite to
> https://example.com/view.php?id=video5353

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,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list