Michael Heuberger
Michael Heuberger

Reputation: 872

Nginx: How to append a single query parameter

Whenever it's a bot, I'd like to append a single query parameter at the end of the URL in nginx.

So that URLs like https://videomail.io/11e4-38ba-b1a12cc0-8849-cbb56781aee9/ will become https://videomail.io/11e4-38ba-b1a12cc0-8849-cbb56781aee9/?crawl=1

Or without the slash at the end https://videomail.io/11e4-38ba-b1a12cc0-8849-cbb56781aee9 will result into https://videomail.io/11e4-38ba-b1a12cc0-8849-cbb56781aee9?crawl=1

If the parameter crawl already exist, no modification is wanted. How can I do that in nginx?

I tried with this but it didn't work

if ($crawling = 1) {
    rewrite ^(.*)$ $1?crawl=1? break;
}

Any clues?

Upvotes: 1

Views: 1994

Answers (1)

tomasea
tomasea

Reputation: 26

I think you need to modify like this:

if ($arg_crawl = 1 ){
        rewrite ^(.*)$ $1?crawl=1 break;
}

nginx is not surport to check if exist to a parameter,if you want archive the goal,you can try the openresty,use some lua code to do it; http://wiki.nginx.org/HttpLuaModule basiclly like this: set_by_lua $crawl 'if ngx.var.crawl == nil then return 1 else return 0 end';

if ( $crawl = 1 ){
    rewrite ^(.*)$ $1?crawl=1 break;
}
if ( $crawl =0 ){
   set 
}

Upvotes: 1

Related Questions