# Permanent Redirect on nginx

**URL:** <https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536>\
**Category:** NGINX\
**Created:** [April 28, 2016, 4:47pm UTC](https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536 "2016-04-28T16:47:46Z")\
**Posts on this page:** 4\
**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:** [April 28, 2016, 4:47pm UTC](https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536/1 "2016-04-28T16:47:46Z")

</div>

Dear Nginx Community

My org has changed domain from unam.na to unam.edu.na. I intend to  
effect a permanent redirect on Nginx.

I have tried the lines in bold below but browsers are complaining of too  
many redirects.

Any tips on how I can adjust the config file for permanent redirect?

-----------------------Complete config file under  
sites-enabled---------------------------------------

server {

listen 80;  
root /usr/share/nginx/atom;

# [http://wiki.nginx.org/HttpCoreModule#server\_name](http://wiki.nginx.org/HttpCoreModule#server_name)

# \_ means catch any, but it’s better if you replace this with your

server

# name, e.g. [archives.foobar.com](http://archives.foobar.com)

server\_name \_;  
#server\_name archives.unam.na;  
#return 301 $scheme://archives.unam.edu.na;

#server\_name archives.unam.na;  
#rewrite ^ http://archives.unam.edu.na$request\_uri? permanent;

client\_max\_body\_size 72M;

# [http://wiki.nginx.org/HttpCoreModule#try\_files](http://wiki.nginx.org/HttpCoreModule#try_files)

location / {  
try\_files $uri /index.php?$args;  
}

location ~ /. {  
deny all;  
return 404;  
}

location ~\* (.yml|.ini|.tmpl)$ {  
deny all;  
return 404;  
}

location ~\* /(?:uploads|files)/.\*.php$ {  
deny all;  
return 404;  
}

# This is the most important part, as here we are redirecting some

specific

# requests to PHP-FPM so PHP can do its job passing data to and from

the

# web server.

location ~ ^/(index|qubit\_dev).php(/|$) {  
include /etc/nginx/fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_split\_path\_info ^(.+.php)(/.\*)$;  
fastcgi\_pass unix:/var/run/php5-fpm.atom.sock;  
}

location ~\* .php$ {  
deny all;  
return 404;  
}

}

---

<div class="post-metadata">

**Author:** ![MumanyiS\_Bravismore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@MumanyiS\_Bravismore](https://www.ruby-forum.com/u/MumanyiS_Bravismore)\
**Post date:** [April 28, 2016, 7:15pm UTC](https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536/2 "2016-04-28T19:15:34Z")

</div>

On Thu, Apr 28, 2016 at 02:47:03PM +0000, Mumanyi, Bravismore wrote:

Hi there,

> My org has changed domain from unam.na to unam.edu.na. I intend to effect a  
> permanent redirect on Nginx.

Have two server{} blocks.

The one with “server\_name new-name;” should be the fuller one, doing  
everything you want.

The other should have “server\_name old-name;” and “return 301  
http://new-name$request\_uri;” and not a lot more.

## f

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

---

<div class="post-metadata">

**Author:** ![MumanyiS\_Bravismore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@MumanyiS\_Bravismore](https://www.ruby-forum.com/u/MumanyiS_Bravismore)\
**Post date:** [April 29, 2016, 9:33am UTC](https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536/3 "2016-04-29T09:33:41Z")

</div>

Francis, your config did magic! Kudos…

However, I just wondering the cause for the redirect URL having two  
trailing slashes “//” i.e. “[Welcome to UNAM Archives - UNAM Archives](http://archives.unam.edu.na//)”? The links  
seem to work though.

The two server blocks are as follows.

server {  
listen 80;  
listen 443 ssl;  
server\_name archives.unam.na;  
return 301 $scheme://archives.unam.edu.na$request\_uri;  
}

server {

listen 80;  
listen 443 ssl;  
root /usr/share/nginx/atom;

# [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#server_name)

# \_ means catch any, but it’s better if you replace this with your

server

# name, e.g. [archives.foobar.com](http://archives.foobar.com)

#server\_name \_;  
server\_name archives.unam.edu.na;

Regards

---

<div class="post-metadata">

**Author:** ![MumanyiS\_Bravismore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@MumanyiS\_Bravismore](https://www.ruby-forum.com/u/MumanyiS_Bravismore)\
**Post date:** [April 30, 2016, 9:37am UTC](https://www.ruby-forum.com/t/permanent-redirect-on-nginx/244536/4 "2016-04-30T09:37:07Z")

</div>

On Fri, Apr 29, 2016 at 07:33:04AM +0000, Mumanyi, Bravismore wrote:

Hi there,

> Francis, your config did magic! Kudos…

Good to hear you have it working, thanks.

> However, I just wondering the cause for the redirect URL having two trailing  
> slashes “//” i.e. “[Welcome to UNAM Archives - UNAM Archives](http://archives.unam.edu.na//)”? The links seem to work though.

If you are reporting that

curl -i [http://archives.unam.na/](http://archives.unam.na/)

returns a http 301 with a Location: header of  
[Welcome to UNAM Archives - UNAM Archives](http://archives.unam.edu.na//) then that’s not what I expect and that’s  
not what I observe.

If you’re reporting something else that is repeatable when there is no  
extra caching in the browser, for example, then the curl command that  
shows the request and response will be helpful.

Cheers,

## f

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