4

among all 'redirect in nginx' questions I couldn't find how to redirect (using return 301 and better no ifs) using regexps.

I have a link to my website and I'd like to remove parameter a the end:

domain.com/article/some-sluggish-link/?report=1        #number at end

Regex to find this:

\?report=\d*$

For this i want 301 redirect to:

domain.com/article/some-sluggish-link/

I nginx.conf I have 3 redirections:

server {        
    listen 80;      
    server_name subdomain.example.com.; #just one subdomain 
    }

server {
    listen 80;
    server_name  *.example.com;         
    return 301 http://example.com$request_uri;
    }

server {
    listen 80;
    server_name  example.com;
    }

and it works; it redirects 301 all www., ww., aaa., and every subdomain, except 1 particular, to main domain.com

I'd appreciate any help Cheers!

EDIT 25/03/2015

I already have "location /" in my conf file:

location / {  
    uwsgi_pass unix://opt/run/ps2.sock;  
    include uwsgi_params;  
    }

which redirects to some django app. After applying 'if' clause it gives me infinite loop!!!

My problem is basically with SEO, meaning google indexes some certain pages (those with '?report=' parameter) that are copies of urls without this trailing parameter.

I wanted googlebot to stop indexing using robots.txt, but the problem is you can't use regexps in this file. Also I can't say which url exactly nedd to be redirected or stopped from indexing cause it happens somehow randomly...

0

1 Answer 1

1

I haven't tried this myself, but it should work. Add this inside your server {} block:

location / {
    if ($args !~ ^$) {
        rewrite ^ $request_uri? permanent;
    }
}

What this block actually does:

location / tells nginx to apply these directives to all requests matching root directory and subdirectories.

if ($args !~ ^$) checks if the URI contains any query parameters with regular expression.

rewrite ^ $request_uri? permanent; makes the redirect to the desired URI without any query parameters. The ? at the end of $request_uri tells nginx to strip query parameters from the redirect URL.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .