Welcome! Log In Create A New Profile

Advanced

Re: Rewrite rules not working

Francis Daly
September 11, 2016 09:46AM
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@daoine.org

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Rewrite rules not working

khav September 11, 2016 08:12AM

Re: Rewrite rules not working

Francis Daly September 11, 2016 09:46AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 145
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready