Rewrite rules with NGinx

Mik J mikydevel at yahoo.fr
Sun Jan 20 15:22:07 UTC 2013



Hello,

I have read the thread "Redirect specific query to a page" carefully as I need something similar but I've also searched on different websites and they don't use the map solution.

Action 1:
I would like that when people access to www.domain.org/nginx the system queries the webpage www.domain.org/page.php?arg=nginx
With Apache I did
RewriteRule ^nginx$ /page.php?arg=nginx [L]
For Nginx I found this equivalent
rewrite ^nginx$ /page.php?arg=nginx last;

Action 2:
For people who try to access to www.domain.org/page.php?arg=nginx, they are redirected to www.domain.org/nginx
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^arg=nginx$
RewriteRule ^page\.php$ /nginx? [R=302,L]

When I wrote these rules with Apache I had to add
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
to prevent the loop

How could I write this second action with NGinx ?
Is my first action rule correct ?
When should I use map ?







>________________________________
> De : Vadim Lazovskiy <vadim.lazovskiy at gmail.com>
>À : nginx at nginx.org 
>Envoyé le : Dimanche 20 janvier 2013 14h00
>Objet : Re: Redirect specific query to a page
> 
>
>Hello,
>
>
>http://nginx.org/en/docs/http/ngx_http_map_module.html
>
>Before version 0.9.0 only a single variable could be specified in the first parameter.
>
>
>
>map $arg_option$arg_view$arg_id$arg_itemid $redirect_uri {
>      com_contentarticle40106 /blahblah/page1;
>      com_contentarticle164139 /blahblah/page2;
>}
>
>
>...
>
>
>location / {
>      if ($redirect_uri) {
>            return 301 $scheme://$host$redirect_uri;
>      }
>}
>
>
>It's also order-independent. And this is an Orthodox way :)
>
>
>
>2013/1/20 António P. P. Almeida <appa at perusio.net>
>
>On 20 Jan 2013 02h28 CET, ondanomala_albertelli at yahoo.it wrote:
>>
>>> I tried it but I get the error "nginx: [emerg] "set" directive is
>>> not allowed here" (as you said I put set and map at http level and
>>> rewrite at server level).
>>>
>>
>>Indeed. set is not allowed at the http level, although it could be
>>quite useful IMHO. Either that or allowing to combine variables as the
>>left side of the map.
>>
>>You have to do a more complex mapping. Remove the set and use the
>>following map instead (while mantaining the if at the server level):
>>
>>map $query_string $new_uri {
>>    option=com_content&view=category&id=40&Itemid=106 /blahblah/page1;
>>    option=com_content&view=article&id=164&Itemid=139 /blahblah/page2;
>>    ## add as query string -> new uri lines as needed.
>>}
>>
>>Now it depends on the order, which is bad. Other option is to use only
>>set and if. Like this:
>>
>>At the server level:
>>
>>
>>## String composed of all the arguments on the URI.
>>set $arg_str $arg_option$arg_view$arg_id$arg_itemid;
>>
>>if ($arg_str = com_contentarticle40106) {
>>   rewrite ^ $scheme://$host/blahblah/page1 permanent;
>>}
>>
>>if ($arg_str = com_contentarticle164139) {
>>   rewrite ^ $scheme://$host/blahblah/page2 permanent;
>>}
>>
>>Add as many if blocks as needed.
>>
>>It's ugly but since you have only a few redirects, it's manageable
>>IMHO. Now you no longer depend on the order.
>>
>>
>>--- appa
>>
>>_______________________________________________
>>nginx mailing list
>>nginx at nginx.org
>>http://mailman.nginx.org/mailman/listinfo/nginx
>>
>
>
>
>-- 
>
>Best Regards,
>
>Vadim Lazovskiy
>_______________________________________________
>nginx mailing list
>nginx at nginx.org
>http://mailman.nginx.org/mailman/listinfo/nginx
>
>



More information about the nginx mailing list