Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

among all 'redirect in nginx' questions I couldnt find how to redirect (using return 301 and beteer 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.domain.com.; #just one subdomain }

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

server { listen 80; server_name domain.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...

share|improve this question
add comment

1 Answer 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.