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:
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;
}
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.
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.
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.
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/”.
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 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;
}
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:
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?