在 nginx 中缺少斜杠时添加斜杠

vin*_*.io 10 rewrite url nginx magento

我使用以下配置在 Nginx 上运行 Magento:http : //www.magentocommerce.com/wiki/1_-_installation_and_configuration/configuring_nginx_for_magento

现在我想将所有 URL 301 不带尾随斜杠到包含尾随斜杠的对应物。例如:/contacts 到 /contacts/。

我已经尝试了几乎所有我能找到的 nginx 指令,但无济于事。例如,nginx-Rewrite URL with Trailing Slash 中指定的指令会导致重定向到 /index.php/。

我应该添加哪个指令以及在哪里添加?

vin*_*.io 11

我找到了解决方案:我在“location /”-block中的“try_files”指令上方添加了以下行:

rewrite ^([^.]*[^/])$ $1/ permanent;

这有什么神奇之处。


Fle*_*der 2

这是非常非常棘手的,因为您必须考虑 URL 中的所有可能性。让我们仔细看看您在那里发布的配置,并在尝试实现您的愿望时对其进行优化。我必须更正完整的配置,因为它对您的网站包含多个安全风险(并在配置后继续阅读)。

server {
    server_name    DOMAIN.com;
    return         301 $scheme://www.$server_name$request_uri;
}

server {
    index          index.html index.php;
    listen         80 default;
    root           /var/www;
    server_name    www.DOMAIN.com;

    location / {

        # Hide ALL kind of hidden stuff.
        location ~ /\. {
            return 403;
        }

        # Protect Magento's special directories in document root.
        location ~* ^/(app|includes|lib|media/downloadable|pkginfo|report/config\.xml|var)/? {
            return 403;
        }

        # Directly deliver known file types.
        location ~* \.(css|gif|ico|jpe?g|js(on)?|png|svg|webp)$ {
            access_log      off;
            add_header      Cache-Control   "public";
            add_header      Pragma          "public";
            expires         30d;
            log_not_found   off;
            tcp_nodelay     off;
            try_files       $uri =404;
        }

        # Do not allow direct access to index.php
        location ~* ^(.*)index\.php$ {
            return 301 $1;
        }

        # Extremely risky ... oh boy!
        location ~* \.php/ {
            rewrite ^(.*\.php)/ $1 last;
        }

        # Not direct index.php access and not one of those ultra
        # risky php files with a path appended to their script name,
        # let's try to add a slash if it's missing.
        location ~* ^(.*)[^/]+$ {
            return 301 $1/;
        }

        location ~* \.php$ {
          include          fastcgi_params;
          fastcgi_index    index.php;
          fastcgi_param    PATH_INFO          $fastcgi_path_info;
          fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
          fastcgi_param    SCRIPT_NAME        $fastcgi_script_name;
          fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
          fastcgi_param    MAGE_RUN_CODE      "default";
          fastcgi_param    MAGE_RUN_TYPE      "store";
          fastcgi_pass     127.0.0.1:9000;

          # Ensure it's an actual PHP file!
          try_files        $uri =404;
        }
    }

    location ^~ /var/export/ {
        auth_basic              "Restricted";
        auth_basic_user_file    htpasswd;
        autoindex               on;
    }
}
Run Code Online (Sandbox Code Playgroud)

重要的!重要的!重要的!重要的!

我无法测试此配置,我已根据我的最佳知识将其写下来。nginx -t请在尝试使用 nginx 之前执行reload,并报告是否报告任何错误。我再说一遍,不要在你的生产站点上测试这个并测试你能想到的一切。