# Capture rewrite for every after question mark

**URL:** <https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384>\
**Category:** NGINX\
**Created:** [November 30, 2011, 6:06pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384 "2011-11-30T18:06:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [November 30, 2011, 6:06pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/1 "2011-11-30T18:06:46Z")

</div>

Hello,

I have found the following while searching it is the only one i can  
get working with my config setup  
to send a 301 permanently if “/index.php” is directly accessed.

```
if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}

```

GET [http://www.example.com/index.php](http://www.example.com/index.php) 301 Moved Permanently  
GET [http://www.example.com/](http://www.example.com/) 200 OK

* * *

above works as expected.

But, now if i have the following results as 200 OK as expected.

[http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 200 OK

So how do you rewrite that if there is a question mark and or value  
that follows “/index.php?” then returns 301.

GET [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 301 Moved  
Permanently

GET [http://www.example.com/](http://www.example.com/) 200 OK

Posted at Nginx Forum:

> **[capture rewrite for every after question mark](https://forum.nginx.org/read.php?2,219385,219385#msg-219385)**
>
> Hello, I have found the following while searching it is the only one i can get working with my config setup to send a 301 permanently if "/index.php" is directly accessed. if ($request\_uri ~\* ^(/home(/index)?|/index(.php)?)/?$)

---

<div class="post-metadata">

**Author:** ![token](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@token](https://www.ruby-forum.com/u/token)\
**Post date:** [November 30, 2011, 6:13pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/2 "2011-11-30T18:13:37Z")

</div>

On 30 Nov 2011 17h06 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 200 OK
> 
> So how do you rewrite that if there is a question mark and or value  
> that follows “/index.php?” then returns 301.
> 
> GET [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 301 Moved  
> Permanently
> 
> GET [http://www.example.com/](http://www.example.com/) 200 OK

Try:

location = /index.php {  
return 301 /;  
}

— appa

---

<div class="post-metadata">

**Author:** ![token](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@token](https://www.ruby-forum.com/u/token)\
**Post date:** [November 30, 2011, 6:47pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/3 "2011-11-30T18:47:07Z")

</div>

On 30 Nov 2011 17h19 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for suggestion appa, i apply what said to try and results in  
> a “Redirection Loop”.
> 
> > Hello,
> > 
> > I have found the following while searching it is the only one i can  
> > get working with my config setup  
> > to send a 301 permanently if “/index.php” is directly accessed.

Remove/comment out the if below and try:

http {

map $redirect\_direct\_access $request\_uri {  
default 0;  
~/index(.php)? 1;  
~/home(/index)? 1;  
}  
}

server {  
server\_name [www.example.com](http://www.example.com);

```
root /path/to/root;

location / {
    if ($redirect_direct_access) {
       return 301 /;
    }
}

```

}

> if ($request\_uri ~\* ^(/home(/index)?|/index(.php)?)/?$)  
> {  
> rewrite ^(.\*)$ / permanent;  
> }

— appa

---

<div class="post-metadata">

**Author:** ![token](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@token](https://www.ruby-forum.com/u/token)\
**Post date:** [November 30, 2011, 6:52pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/4 "2011-11-30T18:52:04Z")

</div>

> Thanks for suggestion appa, i apply what said to try and results in a  
> “Redirection Loop”.

> }  
> [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 200 OK
> 
> So how do you rewrite that if there is a question mark and or value  
> that follows “/index.php?” then returns 301.
> 
> GET [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 301 Moved  
> Permanently
> 
> GET [http://www.example.com/](http://www.example.com/) 200 OK

> Try:

> location = /index.php {  
> return 301 /;  
> }

Update,

I am using

if ($request\_uri ~\* ^/index.php(._)$){  
rewrite ^(._)$ / permanent;  
}

```
   The above resuls as which dose the 301 moved permanently with the

```

exception of retaining the the question and the the value.

I guess from a search engines point of view its see’s the 301 Moved  
Permanently so in that case it ok, but it would be nice to not retain  
the “?randomvalue”.

```
   http://www.example.com/index.php?randomvalue 301 Moved

```

Permanently  
[Example Domain](http://www.example.com/?randomvalue) 200 OK

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219385,219391#msg-219391](http://forum.nginx.org/read.php?2,219385,219391#msg-219391)

---

<div class="post-metadata">

**Author:** ![token](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@token](https://www.ruby-forum.com/u/token)\
**Post date:** [November 30, 2011, 6:19pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/5 "2011-11-30T18:19:49Z")

</div>

Thanks for suggestion appa, i apply what said to try and results in a  
“Redirection Loop”.

> }  
> [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 200 OK
> 
> So how do you rewrite that if there is a question mark and or value  
> that follows “/index.php?” then returns 301.
> 
> GET [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 301 Moved  
> Permanently
> 
> GET [http://www.example.com/](http://www.example.com/) 200 OK

> Try:

> location = /index.php {  
> return 301 /;  
> }

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219385,219388#msg-219388](http://forum.nginx.org/read.php?2,219385,219388#msg-219388)

---

<div class="post-metadata">

**Author:** ![token](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@token](https://www.ruby-forum.com/u/token)\
**Post date:** [November 30, 2011, 7:13pm UTC](https://www.ruby-forum.com/t/capture-rewrite-for-every-after-question-mark/214384/6 "2011-11-30T19:13:04Z")

</div>

## token Wrote:

> > GET [http://www.example.com/](http://www.example.com/) 200 OK
> > 
> > Try:  
> > rewrite ^(.\*)$ / permanent;
> 
> [http://www.example.com/index.php?randomvalue](http://www.example.com/index.php?randomvalue) 301  
> Moved Permanently  
> [Example Domain](http://www.example.com/?randomvalue) 200 OK

Final Update, Resolved

if ($request\_uri ~\* ^/index.php(._)$){  
rewrite ^(._)$ /? permanent;  
}

Seems to work now

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,219385,219392#msg-219392](http://forum.nginx.org/read.php?2,219385,219392#msg-219392)
