在nginx中缺less尾部斜线

我使用这个configuration在Nginx上运行Magento: http : //www.magentocommerce.com/wiki/1_-_installation_and_configuration/configuring_nginx_for_magento 。

现在我想301所有的url都没有跟踪斜线到包含斜线的对方。 例如:/ contacts to / contacts /。

我试过几乎所有的nginx指令,我都能find,但无济于事。 例如,在nginx中指定的指令 – 使用拖尾斜杠重写URL会导致redirect到/index.php/。

我应该添加哪个指令?

我find了解决scheme:我在“location /”块中的“try_files”指令上添加了以下行:

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

这是神奇的。

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

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

重要! 重要! 重要! 重要!

我无法testing这个configuration,我已经写下来,以我的最佳知识。 在尝试reload nginx -t之前,请执行nginx -t ,如果报告有错误,请回报。 不要,我再说一遍,不要在你的生产现场testing这个,并testing你所能想到的一切。