Forum: NGINX Rewrite all directory URLs with certain exceptions

Posted by Cristian R. (cristian_r)
on 2012-11-28 09:05
(Received via mailing list)
Hello

I have urls like example.com/123
I need them rewritten to example.com/?v=123

However, I want to skip certain directories from rewrite
eg.
example.com/status
example.com/admin

right now I have this:

if (!-e $request_filename){
    rewrite ^/([A-Za-z0-9-]+)/?$ 
http://www.example.com/v.php?dl=$1redirect;
}

Problem is that all urls are rewritten.. how do I put some exceptions?

Thanks for any suggestion
Posted by Edho Arief (Guest)
on 2012-11-28 09:07
(Received via mailing list)
On Wed, Nov 28, 2012 at 3:04 PM, Cristian Rusu <crirus@gmail.com> wrote:
> right now I have this:
>
> if (!-e $request_filename){
>     rewrite ^/([A-Za-z0-9-]+)/?$ http://www.example.com/v.php?dl=$1
> redirect;
> }
>
> Problem is that all urls are rewritten.. how do I put some exceptions?
>

put the rewrite in a location block (e.g. inside location / { }).
Posted by Maxim Dounin (Guest)
on 2012-11-28 09:31
(Received via mailing list)
Hello!

On Wed, Nov 28, 2012 at 10:04:58AM +0200, Cristian Rusu wrote:

> right now I have this:
>
> if (!-e $request_filename){
>     rewrite ^/([A-Za-z0-9-]+)/?$ http://www.example.com/v.php?dl=$1redirect;
> }
>
> Problem is that all urls are rewritten.. how do I put some exceptions?

I would recommend using location matching to differentiate URIs
which should be handled differently.  E.g.

    location / {
        # you may want to use try_files here instead
        if (...) {
            rewrite ...
        }
        ...
    }

    location /status {
        ...
    }

    location /admin {
        ...
    }

See http://nginx.org/r/location for more information.

--
Maxim Dounin
http://nginx.com/support.html
Posted by Cristian R. (cristian_r)
on 2012-11-28 13:11
(Received via mailing list)
Excellent, it worked

I have another urgent matter on a server live we just switched to

I need that links like this:
http://www.example.com/?v=JYH253CT

to be rewritten as http://www.example.com/v.php?dl=JYH253CT


Please help, server is down :(

---------------------------------------------------------------
Cristian Rusu
Web Developement & Electronic Publishing

======
Crilance.com
Crilance.blogspot.com
Posted by Edho Arief (Guest)
on 2012-11-28 13:26
(Received via mailing list)
On Wed, Nov 28, 2012 at 7:11 PM, Cristian Rusu <crirus@gmail.com> wrote:
> Please help, server is down :(
>

http://nginx.org/en/docs/http/ngx_http_core_module...

Maybe something like this.

location = / {
  if ($arg_v) {
    rewrite ^ /v.php?dl=$arg_v;
  }
}
Posted by Cristian R. (cristian_r)
on 2012-11-28 13:34
(Received via mailing list)
On Wed, Nov 28, 2012 at 2:25 PM, Edho Arief <edho@myconan.net> wrote:

> >
>   }
> }
>

I tried this from a htaccess to nginx converter

if ($query_string ~ "^v=(.*)$"){
    rewrite ^/index\.php$ /v.php?dl=$1 break;
}

But the resulting url is /v.php?dl=%1&v=FER34S

beats me!!
Posted by Antonio P.P. Almeida (Guest)
on 2012-11-28 13:59
(Received via mailing list)
>> > to be rewritten as http://www.example.com/v.php?dl=JYH253CT
>>   if ($arg_v) {
>>     rewrite ^ /v.php?dl=$arg_v;
>>   }
>> }
>>
>
> I tried this from a htaccess to nginx converter
>
> if ($query_string ~ "^v=(.*)$"){
>     rewrite ^/index\.php$ /v.php?dl=$1 break;
> }

Try:

if ($arg_v) {
    rewrite ^ /v.php?dl=$arg_v break;
}

--appa
Posted by Cristian R. (cristian_r)
on 2012-11-28 14:04
(Received via mailing list)
Yes, this worked, I was also trying to get an idea of that matching in 
my
attempt...

Why is $1 not matching right?

---------------------------------------------------------------
Cristian Rusu
Web Developement & Electronic Publishing

======
Crilance.com
Crilance.blogspot.com
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.