# Rewrite to HTTPS?

**URL:** <https://www.ruby-forum.com/t/rewrite-to-https/185290>\
**Category:** NGINX\
**Created:** [March 17, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/rewrite-to-https/185290 "2010-03-17T15:25:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5fc32e/32.png) [@Guillaume\_F](https://www.ruby-forum.com/u/Guillaume_F)\
**Post date:** [March 17, 2010, 3:25pm UTC](https://www.ruby-forum.com/t/rewrite-to-https/185290/1 "2010-03-17T15:25:21Z")

</div>

Hi all,

I’d like to make a rewrite rule to redirect the client to an HTTPS page,  
so that a user is redirected from:  
[http://host.name/p/A2010/TExfDqN5cPi/b7i8UDhqgx3rmXlc+yv+T7hmxuykWGENpg==](http://host.name/p/A2010/TExfDqN5cPi/b7i8UDhqgx3rmXlc+yv+T7hmxuykWGENpg==)  
to:  
[https://my.host.name/path/to/profil/?a=A2010&c=TExfDqN5cPi/b7i8UDhqgx3rmXlc%2Byv%2BT7hmxuykWGENpg==](https://my.host.name/path/to/profil/?a=A2010&c=TExfDqN5cPi/b7i8UDhqgx3rmXlc%2byv%2bT7hmxuykWGENpg==)  
but I’m getting a “400 Bad Request” from nginx when I try to send them  
to the HTTPS site.

This works (returns a “302 Moved Temporarily”):  
rewrite “^/p/([AH][0-9]{4})/(.+)$”  
[http://my.host.name/path/to/profil/?a=$1&c=$2](http://my.host.name/path/to/profil/?a=$1&c=$2) break;

This doesn’t (returns a “400 Bad Request”):  
rewrite “^/p/([AH][0-9]{4})/(.+)$”  
[https://my.host.name/path/to/profil/?a=$1&c=$2](https://my.host.name/path/to/profil/?a=$1&c=$2) break;

Could someone shed some light on this?

Thanks a lot!  
GFK’s

Here’s my sanitized config:  
server {  
listen 1.2.3.4:80;  
server\_name my.host.name;  
proxy\_connect\_timeout 2;  
client\_max\_body\_size 4M;  
access\_log /var/log/nginx/my.host.name\_HTTP.access.log;  
error\_log /var/log/nginx/my.host.name\_HTTP.error.log;

location / {  
proxy\_next\_upstream error timeout invalid\_header http\_500;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;

```
# Profil
rewrite "^/p/([AH][0-9]{4})/(.+)$"

```

[https://my.host.name/path/to/profil/?a=$1&c=$2](https://my.host.name/path/to/profil/?a=$1&c=$2) break;

```
proxy_pass http://BACKEND;

```

}  
}

server {  
listen 1.2.3.4:443;  
server\_name my.host.name;  
client\_max\_body\_size 4M;  
access\_log /var/log/nginx/my.host.name\_HTTPS.access.log;  
error\_log /var/log/nginx/my.host.name\_HTTPS.error.log;

ssl on;  
ssl\_certificate /etc/nginx/certs/my.host.name.crt;  
ssl\_certificate\_key /etc/nginx/certs/my.host.name.pem;  
ssl\_protocols SSLv3 TLSv1; ssl\_ciphers  
ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP;

location / {  
proxy\_next\_upstream error timeout invalid\_header http\_500;  
proxy\_set\_header SSL Enabled;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;

```
proxy_pass http://BACKEND;

```

}  
}

---

<div class="post-metadata">

**Author:** ![Guillaume\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5fc32e/32.png) [@Guillaume\_F](https://www.ruby-forum.com/u/Guillaume_F)\
**Post date:** [March 17, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/rewrite-to-https/185290/2 "2010-03-17T16:01:04Z")

</div>

Silly me, should have RTFM:  
[http://wiki.nginx.org/NginxHttpRewriteModule#rewrite](http://wiki.nginx.org/NginxHttpRewriteModule#rewrite)

This fixes the problem:  
rewrite “^/p/([AH][0-9]{4})/(.+)$”  
[https://my.host.name/path/to/profil/?a=$1&c=$2](https://my.host.name/path/to/profil/?a=$1&c=$2) redirect;

Guillaume F. a Ã©crit :
