# Remove query string on rewrite

**URL:** <https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156>\
**Category:** NGINX\
**Created:** [August 12, 2010, 8:33am UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156 "2010-08-12T08:33:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![celevra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@celevra](https://www.ruby-forum.com/u/celevra)\
**Post date:** [August 12, 2010, 8:33am UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156/1 "2010-08-12T08:33:50Z")

</div>

Hi,  
I am trying to make a 301 redirect like this:  
/authors.php?name=xxx ==\> /author/xxx

this config solves the problem partially -

```
    location ~ ^/authors.php {
             if ($args ~ "name=(.+)" ) {
                       set $arg_name $1;
                      rewrite ^/authors\.php "/author/$arg_name"

```

permanent;  
}  
}

But the query string remains, and the final location is  
/author/xxx?name=xxx

How can i remove the unneeded query string?

Thank you!

Posted at Nginx Forum:

> **[Remove query string on rewrite](https://forum.nginx.org/read.php?2,118933,118933#msg-118933)**
>
> Hi, I am trying to make a 301 redirect like this: /authors.php?name=xxx ==\> /author/xxx this config solves the problem partially - location ~ ^/authors.php { if ($args ~ "name=(.+)" ) {

---

<div class="post-metadata">

**Author:** ![celevra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@celevra](https://www.ruby-forum.com/u/celevra)\
**Post date:** [August 12, 2010, 11:59am UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156/2 "2010-08-12T11:59:20Z")

</div>

2010/8/12 celevra [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> permanent;  
> Â Â Â Â Â Â Â Â }  
> Â Â Â Â }
> 
> But the query string remains, and the final location is  
> /author/xxx?name=xxx
> 
> How can i remove the unneeded query string?

You should add a question mark at the end of the rewrite:

```
    location ~ ^/authors.php {
             if ($args ~ "name=(.+)" ) {
                       set $arg_name $1;
                      rewrite ^/authors\.php "/author/$arg_name?" 

```

permanent;  
}  
}

there is a cleaner way to write this, but note that this way $arg\_name  
would not be URL encoded:

location ~ ^/authors.php {  
if ($arg\_name) {  
rewrite ^/authors.php /author/$arg\_name? permanent;  
}  
}

---

<div class="post-metadata">

**Author:** ![celevra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@celevra](https://www.ruby-forum.com/u/celevra)\
**Post date:** [August 12, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156/3 "2010-08-12T12:01:40Z")

</div>

Hello!

On Thu, Aug 12, 2010 at 02:32:56AM -0400, celevra wrote:

> permanent;  
> }  
> }
> 
> But the query string remains, and the final location is  
> /author/xxx?name=xxx
> 
> How can i remove the unneeded query string?

Add trailing “?” to replacement string.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![celevra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@celevra](https://www.ruby-forum.com/u/celevra)\
**Post date:** [February 8, 2012, 2:55am UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156/4 "2012-02-08T02:55:20Z")

</div>

Thank Maxim D. for take care, I noted, and try now  
tyanhly

Posted at Nginx Forum:

> **[Re: Remove query string on rewrite](https://forum.nginx.org/read.php?2,118933,222092#msg-222092)**
>
> Hi, I am trying to make a 301 redirect like this: /authors.php?name=xxx ==\> /author/xxx this config solves the problem partially - location ~ ^/authors.php { if ($args ~ "name=(.+)" ) {

---

<div class="post-metadata">

**Author:** ![celevra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@celevra](https://www.ruby-forum.com/u/celevra)\
**Post date:** [February 8, 2012, 2:44am UTC](https://www.ruby-forum.com/t/remove-query-string-on-rewrite/193156/5 "2012-02-08T02:44:51Z")

</div>

Thank Mikhail Mazursky, I try now.

tyanhly

Posted at Nginx Forum:

> **[Re: Remove query string on rewrite](https://forum.nginx.org/read.php?2,118933,222091#msg-222091)**
>
> Hi, I am trying to make a 301 redirect like this: /authors.php?name=xxx ==\> /author/xxx this config solves the problem partially - location ~ ^/authors.php { if ($args ~ "name=(.+)" ) {
