我正在为 nginx 的配置而苦恼。我必须将后缀添加.html
到所有没有后缀的 URL,然后执行 301 重定向到带有后缀的相同 URL。.html
不应更改带有后缀的 URL。
例如:
- https://my.server.org/some-page=> 301 =>https://my.server.org/some-page.html
- https://my.server.org/some-page.html=> 无变化/重定向,显示基于 php 的页面
任何帮助都将不胜感激!
这是我当前的 nginx 配置:
server {
listen 443 ssl spdy;
server_name my.server.org;
root /var/www/Web;
index index.html index.php;
client_max_body_size 2G;
# Prevent google indexing of staging environment
add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
location / {
try_files $uri $uri/ /index.php?$args;
}
include /etc/nginx/server-config/nginx-static-resources.conf;
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 600s;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
答案1
在您的配置块中添加类似下面的内容Location /
。
例如:
location / {
try_files $uri $uri/ /index.php?$args;
if ($request_filename ~* ^.+.html$) {
break;
}
# add .html to URI and serve file
if (-e $request_filename.html) {
rewrite ^/(.*)$ /$1.html last;
break;
}
}
确认它是否有效,否则将帮助您做其他事情。
注意:如果不影响您的申请,您也可以尝试到try_files $uri $uri/ /index.php?$args;
外面去。Location /