Rewrite all directory URLs with certain exceptions

Maxim Dounin mdounin at mdounin.ru
Wed Nov 28 08:30:35 UTC 2012


Hello!

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

> 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?

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



More information about the nginx mailing list