# Problem with return 302 redirection, with Nginx 1.3.11 + Drupal 7

**URL:** <https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683>\
**Category:** NGINX\
**Created:** [January 23, 2013, 12:46pm UTC](https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683 "2013-01-23T12:46:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 23, 2013, 12:46pm UTC](https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683/1 "2013-01-23T12:46:51Z")

</div>

Hello,

In order to redirect certain Drupal 7 functions to https I have  
setup Nginx 1.3.11 as follows:

location ~\* ^/(?q=)?(?:user|admin|contact$) {  
return 302 https://$host$request\_uri;  
}

# and then the usual:

location / {  
try\_files $uri $uri/ /index.php?$args;  
}  
location = /index.php {  
…  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_pass …  
}

While this works if called as:  
[http://example.com/user](http://example.com/user) → becomes [https://example.com/user](https://example.com/user)

This fails if called as:  
[Example Domain](http://example.com/?q=user) → stays [Example Domain](http://example.com/?q=user)

What should I do to get [Example Domain](http://example.com/?q=user) redirected to  
[https://example.com/user](https://example.com/user) or, if that is not possible, to  
[Example Domain](https://example.com/?q=user) ?

Thank you,  
M.

---

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [January 23, 2013, 1:04pm UTC](https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683/2 "2013-01-23T13:04:24Z")

</div>

On Wed, Jan 23, 2013 at 11:46:23AM +0000, Mark A. wrote:

Hi there,

> location ~\* ^/(?q=)?(?:user|admin|contact$) {  
> return 302 https://$host$request\_uri;  
> }

That probably won’t match the request that you want it to match.

> What should I do to get [Example Domain](http://example.com/?q=user) redirected to  
> [https://example.com/user](https://example.com/user) or, if that is not possible, to  
> [Example Domain](https://example.com/?q=user) ?

The request [Example Domain](http://example.com/?q=user) has location = /, and  
$query\_string  
= q=user, and $arg\_q = user.

So you should use some combination of those variables within

location = / {}

to do the redirection.

Use $arg\_q if you don’t care about any other parts of the query string.  
If  
you have many things to compare, creating a “map” is probably  
worthwhile.

And you’ll also want to consider what to do in that location if $arg\_q  
is not one that you want to redirect – possibly just letting it fall  
through to the “index” value will do.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [January 23, 2013, 2:13pm UTC](https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683/3 "2013-01-23T14:13:57Z")

</div>

On 23 Jan 2013 12h46 CET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> In order to redirect certain Drupal 7 functions to https I have  
> setup Nginx 1.3.11 as follows:
> 
> location ~\* ^/(?q=)?(?:user|admin|contact$) {  
> return 302 https://$host$request\_uri;  
> }

Locations don’t match the query string part.

At the http level:

map $arg\_q $q\_secure {  
default 0;  
~(?:user|admin|contact) 1;  
}

map $uri $u\_secure {  
default 0;  
~^/(?:user|admin|contact) 1;  
}

map $q\_secure$u\_secure $secure {  
default 0;  
10 1;  
01 1;  
}

At the server level:

if ($secure) {  
return 302 https://$host$request\_uri;  
}

Try it.  
— appa

---

<div class="post-metadata">

**Author:** ![larios](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc79bd/32.png) [@larios](https://www.ruby-forum.com/u/larios)\
**Post date:** [January 23, 2013, 9:30pm UTC](https://www.ruby-forum.com/t/problem-with-return-302-redirection-with-nginx-1-3-11-drupal-7/225683/4 "2013-01-23T21:30:44Z")

</div>

On Wed, 23 Jan 2013 14:13:10 +0100, António P. P. Almeida  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > location ~\* ^/(?q=)?(?:user|admin|contact$) {  
> > return 302 https://$host$request\_uri;  
> > }
> 
> Locations don’t match the query string part.

Oh no… bitten again by that characteristic of Location.

One of those (rare) cases that we must use an IF:

# SOLVED: to remove ‘?q=’ from a query use:

if ($args ~ “q=(?.\*)?”) { return 302 $scheme://$host/$q; }

Thank you António.

M.
