2

I have some index.html files sitting in a folder to get some nice urls -

site.com/about

where index.html sits in the about folder. But I am seeing that my site.com/about is being 301 redirected to site.com/about/ I am not sure where the 301 is generated from. It is not in config.

/about/ also has a 301 result.

I guess it makes sense since I am redirecting to the index.html file but should it not be a rewrite? Is there a way to return 200 for /about instead of 301 to about/?

I am using nginx

Server Block:

server {
    listen IP;
    server_name site.com;
    rewrite / $scheme://www.$host$request_uri permanent;    

}

server {
    listen IP:80;
    server_name site.com *.site.com;
    root /var/www/vhosts/site.com/htdocs;
    charset utf-8;
    rewrite_log on;

    location / {
        index index.html index.php;
    try_files $uri $uri/ /$uri.php;
        expires 30d;
    }

    if ($request_uri = /index.php) {
        return 301 $scheme://$host;
    }   
    if ($request_uri = /index) {
        return 301 $scheme://$host;
    } 

    location  /. {
        return 404;
    }
    location ~ .php/ {
        rewrite ^(.*.php)/ $1 last;
    }    
    include "ssl_offloading.inc";
    location ~ .php$ {
#        if (!-e $request_filename) { rewrite / /index.php last; }
        if (!-e $request_filename) { rewrite / /404.php last; }

    }
}
Jon
  • 6,437
  • 8
  • 43
  • 63

1 Answers1

3

The index directive and the $uri/ element of the try_files directive, has the side-effect of adding a trailing / to directory names by performing an external redirect.

To avoid the external redirect and return an appropriate index file when presented with a slash-less directory name, implement the index functionality explicitly within the try_files directive:

location / {
    try_files $uri $uri/index.html $uri.php;
    expires 30d;
}

Notice that .php works only in the last element at this location. If you need to check for $uri/index.php (in addition to $uri.php) you can use a named location block - and move or copy your fastcgi configuration into it.

For example (based on your server block):

root /var/www/vhosts/site.com/htdocs;

error_page 404 /404.php;

location / {
    try_files $uri $uri/index.html @php;
    expires 30d;
}
location @php {
    try_files $uri.php $uri/index.php =404;

    include       fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    ...
    fastcgi_pass ...;
}

location = /index.php { return 301 $scheme://$host; }
location = /index { return 301 $scheme://$host; }
location /. { return 404; }

location ~* \.php(/|$) { rewrite ^(.*)\.php $1 last; }

include "ssl_offloading.inc";
Richard Smith
  • 45,711
  • 6
  • 82
  • 81