Welcome! Log In Create A New Profile

Advanced

Rewrite rules with NGinx

January 20, 2013 10:24AM
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@gmail.com>
>À : nginx@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@perusio.net>
>
>On 20 Jan 2013 02h28 CET, ondanomala_albertelli@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@nginx.org
>>http://mailman.nginx.org/mailman/listinfo/nginx
>>
>
>
>
>--
>
>Best Regards,
>
>Vadim Lazovskiy
>_______________________________________________
>nginx mailing list
>nginx@nginx.org
>http://mailman.nginx.org/mailman/listinfo/nginx
>
>

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

Redirect specific query to a page

OndanomalA January 18, 2013 08:22PM

Re: Redirect specific query to a page

António P. P. Almeida January 18, 2013 08:54PM

Re: Redirect specific query to a page

OndanomalA January 19, 2013 09:46AM

Re: Redirect specific query to a page

António P. P. Almeida January 19, 2013 01:00PM

Re: Redirect specific query to a page

OndanomalA January 19, 2013 08:30PM

Re: Redirect specific query to a page

António P. P. Almeida January 19, 2013 09:44PM

Re: Redirect specific query to a page

Vadim Lazovskiy January 20, 2013 08:02AM

Rewrite rules with NGinx

miky January 20, 2013 10:24AM

Re: Rewrite rules with NGinx

Francis Daly January 20, 2013 10:52AM

Re: Rewrite rules with NGinx

miky January 20, 2013 11:36AM

Re: Rewrite rules with NGinx

Francis Daly January 20, 2013 12:02PM

Re: Rewrite rules with NGinx

António P. P. Almeida January 20, 2013 12:24PM

Re: Redirect specific query to a page

OndanomalA January 20, 2013 07:18AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 88
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready