【问题标题】:Access index.html in folder without 301 redirect在没有 301 重定向的情况下访问文件夹中的 index.html
【发布时间】:2016-05-10 01:36:12
【问题描述】:

我有一些 index.html 文件放在一个文件夹中以获取一些不错的 url -

site.com/about

index.html 位于 about 文件夹中。但是我看到我的 site.com/about 被 301 重定向到 site.com/about/ 我不确定 301 是从哪里生成的。它不在配置中。

/about/ 也有 301 结果。

我想这是有道理的,因为我正在重定向到 index.html 文件,但它不应该是重写吗?有没有办法为 /about 返回 200 而不是 301 到 about/?

我正在使用 Nginx

服务器块:

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

    }
}

【问题讨论】:

  • 请分享您的服务器块的内容。解决方法很简单,但我更愿意给出具体建议。
  • 我投票结束这个问题,因为它属于the site for Professional Wemasters
  • 这是一个配置编程问题而不是 SEO 问题

标签: nginx seo http-status-code-301


【解决方案1】:

index 指令和try_files 指令的$uri/ 元素具有通过执行外部重定向将尾随/ 添加到目录名称的副作用。

为避免外部重定向并在出现无斜杠目录名称时返回适当的索引文件,请在 try_files 指令中显式实现 index 功能:

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

请注意,.php 仅适用于该位置的最后一个元素。如果您需要检查 $uri/index.php(除了 $uri.php),您可以使用命名的位置块 - 并将您的 fastcgi 配置 移动或复制到其中。

例如(基于您的服务器块):

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

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2015-02-09
    • 2016-11-19
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多