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

Igor Sysoev igor at sysoev.ru
Mon Sep 13 22:47:00 MSD 2010


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

> > 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;
> 	}
> }

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:
http://nginx.org/en/docs/http/converting_rewrite_rules.html


-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx mailing list