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

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 → becomes https://example.com/user

This fails if called as:
Example Domain → stays Example Domain

What should I do to get Example Domain redirected to
https://example.com/user or, if that is not possible, to
Example Domain ?

Thank you,
M.

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 redirected to
https://example.com/user or, if that is not possible, to
Example Domain ?

The request Example Domain 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. [email protected]

On 23 Jan 2013 12h46 CET, [email protected] 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

On Wed, 23 Jan 2013 14:13:10 +0100, António P. P. Almeida
[email protected] 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.