# 301 redirect with custom content problem

**URL:** <https://www.ruby-forum.com/t/301-redirect-with-custom-content-problem/224890>\
**Category:** NGINX\
**Created:** [November 30, 2012, 2:53am UTC](https://www.ruby-forum.com/t/301-redirect-with-custom-content-problem/224890 "2012-11-30T02:53:24Z")\
**Posts on this page:** 3\
**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:** [November 30, 2012, 2:53am UTC](https://www.ruby-forum.com/t/301-redirect-with-custom-content-problem/224890/1 "2012-11-30T02:53:24Z")

</div>

Hi

I’m trying to setup a permanent redirect from http → https. As a  
fallback for a small number of users where https is blocked, I’d like to  
show them a message.

I thought an error\_page 301 handler would allow me to do this, but I’m  
having trouble making it work:

server {  
listen \*:80 default;  
rewrite ^ [https://example.com$uri](https://example.com$uri) permanent;  
}

error\_page 301 /foo/bar/301.html;

Seems to confuse the redirect. It seems to completely replace the  
redirect Location header with foo/bar/301.html rather than actually  
serving that content.

I tried some alternatives like:

```
error_page 301 @301;
location @301 {
  root /foo/bar/;
  try_files $uri /301.html;
}

```

But again, it replaces the Location header adding /301.html on the end  
of the redirect hostname rather than actually serving the 301.html  
content.

Is there a reliable way to return custom content for a 301 redirect in a  
way that doesn’t affect the redirect location itself? I’m using nginx  
1.2.4.

–  
Rob M.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Robert\_Mueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Robert\_Mueller](https://www.ruby-forum.com/u/Robert_Mueller)\
**Post date:** [November 30, 2012, 8:44am UTC](https://www.ruby-forum.com/t/301-redirect-with-custom-content-problem/224890/2 "2012-11-30T08:44:03Z")

</div>

Hello!

On Fri, Nov 30, 2012 at 12:53:03PM +1100, Robert Mueller wrote:

> ```
> listen *:80 default;
> rewrite ^ https://example.com$uri permanent;
> 
> ```
> 
> }
> 
> error\_page 301 /foo/bar/301.html;
> 
> Seems to confuse the redirect. It seems to completely replace the  
> redirect Location header with foo/bar/301.html rather than actually  
> serving that content.

This way you’ll end up with two 301 redirects due to rewrite being  
executed again for /foo/bar/301.html.

Try this instead:

```
server {
    listen 80 default;

    location / {
        error_page 301 /foo/bar/301.html;
        return 301 "https://example.com$request_uri";
    }

    location = /foo/bar/301.html {
        # static
    }
}

```

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![Robert\_Mueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Robert\_Mueller](https://www.ruby-forum.com/u/Robert_Mueller)\
**Post date:** [December 4, 2012, 6:29am UTC](https://www.ruby-forum.com/t/301-redirect-with-custom-content-problem/224890/3 "2012-12-04T06:29:50Z")

</div>

> This way you’ll end up with two 301 redirects due to rewrite being  
> executed again for /foo/bar/301.html.

Ah right, makes sense.

> ```
> location = /foo/bar/301.html {
> # static
> }
> }
> 
> ```

Great, that worked, thanks.

Rob
