Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:        #number at end

Regex to find this:


For this i want 301 redirect to:

I nginx.conf I have 3 redirections:

server { listen 80; server_name; #just one subdomain }

server { listen 80; server_name *; return 301$request_uri; }

server { listen 80; server_name; }

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

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

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

Your Answer


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.