# Correct Rewrite?

**URL:** <https://www.ruby-forum.com/t/correct-rewrite/244258>\
**Category:** NGINX\
**Created:** [March 11, 2016, 9:13pm UTC](https://www.ruby-forum.com/t/correct-rewrite/244258 "2016-03-11T21:13:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [March 11, 2016, 9:13pm UTC](https://www.ruby-forum.com/t/correct-rewrite/244258/1 "2016-03-11T21:13:00Z")

</div>

We currently use the following method to perform an http to https  
rewrite.

rewrite ^ https://$server\_name$request\_uri permanent;

I am planning to change it to the preferred method of:

return 301 https://$server\_name$request\_uri;

However, we’d like to also make sure any requests for [domain.com](http://domain.com) are  
sent to  
[www.domain.com](http://www.domain.com), whether someone tries to access [domain.com](http://domain.com) via http or  
https.

How would I write the redirect statement to rewrite http:// to https://  
but  
also rewrite [domain.com](http://domain.com) to [www.domain.com](http://www.domain.com)?

Posted at Nginx Forum:

> **[Correct Rewrite?](https://forum.nginx.org/read.php?2,265257,265257#msg-265257)**
>
> We currently use the following method to perform an http to https rewrite. rewrite ^ https://$server\_name$request\_uri permanent; I am planning to change it to the preferred method of: return 301 https://$server\_name$request\_uri; However, we

---

<div class="post-metadata">

**Author:** ![mevans336](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mevans336](https://www.ruby-forum.com/u/mevans336)\
**Post date:** [March 11, 2016, 9:29pm UTC](https://www.ruby-forum.com/t/correct-rewrite/244258/2 "2016-03-11T21:29:56Z")

</div>

The way I do this is to use multiple server {} blocks, and put all the  
non-canonical hostnames / port 80 requests in a server block with a  
return  
301 to the canonical (and HTTPS) host which only listens on 443 and the  
canonical hostname.

On Fri, Mar 11, 2016 at 9:12 PM, mevans336 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mevans336](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mevans336](https://www.ruby-forum.com/u/mevans336)\
**Post date:** [March 13, 2016, 12:31pm UTC](https://www.ruby-forum.com/t/correct-rewrite/244258/3 "2016-03-13T12:31:15Z")

</div>

That seems like a very elegant way to handle the problem. I’ll give it a  
shot. Thanks!

Posted at Nginx Forum:

> **[Re: Correct Rewrite?](https://forum.nginx.org/read.php?2,265257,265282#msg-265282)**
>
> We currently use the following method to perform an http to https rewrite. rewrite ^ https://$server\_name$request\_uri permanent; I am planning to change it to the preferred method of: return 301 https://$server\_name$request\_uri; However, we
