# Beginner's question: redirecting /dir/index.html to /dir/

**URL:** <https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757>\
**Category:** NGINX\
**Created:** [September 10, 2010, 12:23am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757 "2010-09-10T00:23:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 12:23am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/1 "2010-09-10T00:23:42Z")

</div>

Greetings,

I’m trying to send all …/dir/index.html requests to …/dir/ . I  
looked this up in forums but I’m only able to do it for the root  
directory:

```auto
location / {
    root /var/www/mysite;
    index index.html index.htm;
    if ($request_uri = /index.html) {
    rewrite ^ http://$host? permanent;
    }
}
```

Some day I’ll try to stop this copy-and-paste madness, but “in the  
meantime”… could someone please give me a hand? I would greatly  
appreciate it.

Thanks and cheers,

Ezequiel

Posted at Nginx Forum:

> **[Beginner's question: redirecting /dir/index.html to /dir/](https://forum.nginx.org/read.php?2,128951,128951#msg-128951)**
>
> Greetings, I'm trying to send all .../dir/index.html requests to .../dir/ . I looked this up in forums but I'm only able to do it for the root directory: \[code\] location / { root /var/www/mysite; index index.html index.htm; if

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 3:48am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/2 "2010-09-10T03:48:03Z")

</div>

something like:  
rewrite (.\*)/index.html $1;

should do it.

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 6:00am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/3 "2010-09-10T06:00:20Z")

</div>

On Thu, Sep 09, 2010 at 06:22:23PM -0400, ez77 wrote:

> ```
> if ($request_uri = /index.html) {
> rewrite ^ http://$host? permanent;
> }   
> 
> ```
> 
> }  
> [/code]
> 
> Some day I’ll try to stop this copy-and-paste madness, but “in the  
> meantime”… could someone please give me a hand? I would greatly  
> appreciate it.

You may do it using:

- if ($request\_uri = /index.html) {

- if ($request\_uri ~ /index.html) {

However, using “if ($request\_uri …” means that you should use  
“location”  
instead of “if”. Nevertheless, the right configuration:

```
 location / {
     root /var/www/mysite;
     index index.html;
 }

 location ~ /index.html$ {
     rewrite ^ http://$host/? permanent;
 }

```

does not work, because “index index.html” redirects internally “/” to  
“/index.html” and you will get endless loop with browsers.  
However, you may avoid the internal redirect via try\_files:

```
 location / {
     root /var/www/mysite;
     try_files $uri ${uri}index.html =404;
 }

 location ~ /index.html$ {
     rewrite ^ http://$host/? permanent;
 }

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 6:12am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/4 "2010-09-10T06:12:37Z")

</div>

On Fri, Sep 10, 2010 at 07:58:52AM +0400, Igor S. wrote:

> > ```
> > root /var/www/mysite;
> > 
> > ```
> 
> ```
> index index.html;
> location / {
> root /var/www/mysite;
> try_files $uri ${uri}index.html =404;
> }
> 
> location ~ /index.html$ {
> rewrite ^ http://$host/? permanent;
> }
> 
> ```

The configurations above rerirect to root only.  
Here is the right way:

```
  location / {
      root /var/www/mysite;
      index index.html;
  }

  location ~ ^(/.+/)index.html$ {
      internal;
      error_page 404 http://$host$1?;
  }

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/5 "2010-09-10T21:51:01Z")

</div>

On Fri, Sep 10, 2010 at 12:51:52PM -0400, ez77 wrote:

> internal;  
> error\_page 404 http://$host$1?;  
> }
> 
> , redirects to the root of my site: [site.com/](http://site.com/) instead of [Login | HSTS Redirection Community](http://site.com/dir/).  
> To be precise, to [site.com/](http://site.com/)? . (What’s the point of that “?”?)

Sorry, this “?” is usefull in "rewrite"s only.  
Upgrade to the lastest 0.8.50 and try

location ~ ^(?.\*/)index.html$ {  
internal;  
root /var/www/mysite;  
error\_page 404 http://$host$DIR;  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 11, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/6 "2010-09-11T23:26:42Z")

</div>

> Upgrade to the lastest 0.8.50 and try
> 
> location ~ ^(?.\*/)index.html$ {  
> internal;  
> root /var/www/mysite;  
> error\_page 404 http://$host$DIR;  
> }

Nice! It worked! Thank you very much, once again. At the risk of pushing  
it… is there a way to make the redirect 301 rather than 302?

Cheers,

Ezequiel

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,129575#msg-129575](http://forum.nginx.org/read.php?2,128951,129575#msg-129575)

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 10, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/7 "2010-09-10T18:52:29Z")

</div>

Thank you all for your replies. It’s indeed an honor to receive help  
from the very creator of nginx! Not meaning to bug any of you any  
more… but this last configuration,

location / {  
root /var/www/mysite;  
index index.html;  
}

location ~ ^(/.+/)index.html$ {  
internal;  
error\_page 404 http://$host$1?;  
}

, redirects to the root of my site: [site.com/](http://site.com/) instead of [site.com/dir/](http://site.com/dir/).  
To be precise, to [site.com/](http://site.com/)? . (What’s the point of that “?”?)

Thank you again,

Ezequiel

Posted at Nginx Forum:

> **[Re: Beginner's question: redirecting /dir/index.html to /dir/](https://forum.nginx.org/read.php?2,128951,129247#msg-129247)**
>
> Greetings, I'm trying to send all .../dir/index.html requests to .../dir/ . I looked this up in forums but I'm only able to do it for the root directory: \[code\] location / { root /var/www/mysite; index index.html index.htm; if

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 12, 2010, 3:43am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/8 "2010-09-12T03:43:37Z")

</div>

rewrite ^(.\*/)index.html http://$host$1 permanent;

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 12, 2010, 3:40am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/9 "2010-09-12T03:40:41Z")

</div>

> At the risk of pushing it… is there a way to make the redirect 301  
> rather than 302?

In case anybody is interested, the following code (very similar to the  
original I found elsewhere) works OK in the latest version, 0.8.50, with  
a 301 response:

```
  location / {
        root /var/www/mysite;
        index index.html index.htm;
        if ($request_uri ~ (.*/)index.html) {
        set $files $1;
        rewrite ^ http://$host$files permanent;
        }
    }

```

By now I don’t even know if I tried this exactly with an older  
version… certainly something like this, but it didn’t work.

Cheers!

Ezequiel

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,129630#msg-129630](http://forum.nginx.org/read.php?2,128951,129630#msg-129630)

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 12, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/10 "2010-09-12T18:13:54Z")

</div>

> rewrite ^(.\*/)index.html http://$host$1 permanent;

Hi Mat,

I like your proposal better, which looks more elegant without the if  
clause, but I don’t know where to include it. Under location / I get a  
redirect loop.

Thanks,

Ezequiel

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,129754#msg-129754](http://forum.nginx.org/read.php?2,128951,129754#msg-129754)

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 12, 2010, 8:10pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/11 "2010-09-12T20:10:59Z")

</div>

On Sun, Sep 12, 2010 at 12:13:11PM -0400, ez77 wrote:

> > rewrite ^(.\*/)index.html http://$host$1 permanent;
> 
> Hi Mat,
> 
> I like your proposal better, which looks more elegant without the if  
> clause, but I don’t know where to include it. Under location / I get a  
> redirect loop.

You should set it on server level.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 9:08am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/12 "2010-09-13T09:08:21Z")

</div>

> Location / would also work although server is probably better

Must I include an if clause? I still get a redirect loop without one.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,129926#msg-129926](http://forum.nginx.org/read.php?2,128951,129926#msg-129926)

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 9:18am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/13 "2010-09-13T09:18:18Z")

</div>

no you shouldnt need an if clause.

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 7:48am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/14 "2010-09-13T07:48:45Z")

</div>

Location / would also work although server is probably better

Sent from my iPod

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 10:23am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/15 "2010-09-13T10:23:17Z")

</div>

On Mon, Sep 13, 2010 at 03:46:55PM +1000, Splitice wrote:

> Location / would also work although server is probably better

No. If you request “/dir/” with the confuguration

```
 location / {
     rewrite ^(.*/)index.html http://$host$1 permanent;
     index index.html;
 }

```

then nginx will do an internal redirect to “/dir/index.html”.  
If the redirect will be handled in the “location /”, then  
nginx will rewrite it to an external redirect “[http://host/dir/](http://host/dir/)”.  
And so on.

In this configuration:

```
 rewrite ^(.*/)index.html http://$host$1 permanent;

 location / {
     index index.html;
 }

```

nginx will not run the server level rewrite, after the internal redrect,  
so there will not be the loop.

> > > I like your proposal better, which looks more elegant without the if
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 7:42pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/16 "2010-09-13T19:42:23Z")

</div>

Change ‘permanent’ to ‘redirect’.

Marcus.

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/17 "2010-09-13T19:40:02Z")

</div>

> In this configuration:
> 
> rewrite ^(.\*/)index.html http://$host$1 permanent;
> 
> location / {  
> index index.html;  
> }
> 
> nginx will not run the server level rewrite, after the internal  
> redrect,  
> so there will not be the loop.In this configuration:
> 
> rewrite ^(.\*/)index.html http://$host$1 permanent;\>
> 
> location / {  
> index index.html;  
> }
> 
> nginx will not run the server level rewrite, after the internal  
> redrect,  
> so there will not be the loop.

I still get a redirect this way. Let me point out the “mysite”  
configuration file under /etc/nginx/sites-available/ (too embarrassed to  
give the actual highly-in-construction site):

server {  
listen mysite:80;  
server\_name .mysite;  
if ($host ~\* ..(mysite._)) {  
set $host\_without\_www $1;  
rewrite ^(._)$ http://$host\_without\_www$1 permanent;  
}

access\_log /var/log/nginx/mysite.access.log;

```
rewrite ^(.*/)index.html http://$host$1 permanent;

```

location / {  
root /var/www/mysite;  
index index.html index.htm;  
}  
error\_page 500 502 503 504 /50x.html;  
location = /50x.html {  
root /var/www/mysite;  
}  
}

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,130089#msg-130089](http://forum.nginx.org/read.php?2,128951,130089#msg-130089)

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 13, 2010, 8:47pm UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/18 "2010-09-13T20:47:57Z")

</div>

On Mon, Sep 13, 2010 at 01:39:05PM -0400, ez77 wrote:

> > so there will not be the loop.In this configuration:
> 
> ```
> }
> 
> ```
> 
> location = /50x.html {  
> root /var/www/mysite;  
> }  
> }

Sorry, I was wrong: after an internal redirect nginx runs server level  
rewrite’s. It does not run them if it looks for a new location after  
location  
level rewrite’s. So here is working configuration for 0.8.50:

```
server {
    root /var/www/mysite;

    location / {
        try_files $uri $uri/index.html $uri/index.htm =404;
    }

    location ~ ^(?<DIR>.*/)index.html$ {
        return 301 http://$host$DIR;
    }
}

```

or you may use

```
    location ~ ^(?<DIR>.*/)index.html$ {
        rewrite ^ http://$host$DIR permanent;
    }

```

for 0.7.x and earlier:

```
    location ~ ^.*/index.html$ {
        rewrite ^(.*/)index.html http://$host$1 permanent;
    }

```

As to

```
     if ($host ~* .\.(mysite.*)) {
             set $host_without_www $1;
             rewrite ^(.*)$ http://$host_without_www$1 permanent;
     }

```

please look this:

> **[Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)**

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 14, 2010, 12:53am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/19 "2010-09-14T00:53:15Z")

</div>

Thank you, Igor. I have cleaned up my subdomain redirection the way it  
should be. Moreover, your instructions for “dir/index.html → dir/” work  
under version 0.8.50.

As the whole point of my pet peeve was to have unique, canonical URLs  
for every document, I found it surprising that now URLs such as …/dir  
are not automatically rewritten (301) as …/dir/ . I know I’m a royal  
pain… but (if you don’t mind) what is the difference now?

Thanks again!

Ezequiel

Posted at Nginx Forum:

> **[Re: Beginner's question: redirecting /dir/index.html to /dir/](https://forum.nginx.org/read.php?2,128951,130146#msg-130146)**
>
> Greetings, I'm trying to send all .../dir/index.html requests to .../dir/ . I looked this up in forums but I'm only able to do it for the root directory: \[code\] location / { root /var/www/mysite; index index.html index.htm; if

---

<div class="post-metadata">

**Author:** ![ez77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/fbc32d/32.png) [@ez77](https://www.ruby-forum.com/u/ez77)\
**Post date:** [September 14, 2010, 1:28am UTC](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757/20 "2010-09-14T01:28:24Z")

</div>

> Can the catch-all virtual host redirect with 301 instead of 302?

I got this one: adding “permanent” after the rewrite directive… Sorry.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,128951,130156#msg-130156](http://forum.nginx.org/read.php?2,128951,130156#msg-130156)

[Next page](https://www.ruby-forum.com/t/beginners-question-redirecting-dir-index-html-to-dir/194757.md?page=2)
