我必须通过永久重定向 301 删除我的网站多个 URL 中重复的子目录。
我需要 Nginx 配置方面的帮助。
例如:
http://example.com/foo/DIR/DIR/bar
-->http://example.com/foo/DIR/bar
location ~ ^(.*)/DIR/DIR(.*)$ {
rewrite ^(.*)$ /DIR/$1 last;
break;
}
我当前的 nginx 服务器配置:
server {
listen 443 ssl http2 fastopen=500 reuseport;
listen [::]:443 ssl http2 fastopen=500 reuseport ipv6only=on;
server_name www.example.com;
resolver 1.1.1.1 1.0.0.1 valid=300s;
charset utf-8;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/nginx/snippets/diffie-hellman;
root /var/www/webdisk/example.com/htdocs;
autoindex off;
index load.php index.php index.html;
include /etc/nginx/snippets/security;
include /etc/nginx/snippets/expires;
include /etc/nginx/snippets/error_pages;
include /etc/nginx/snippets/pagespeed;
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php/php7-fpm.sock;
fastcgi_send_timeout 1200s;
fastcgi_read_timeout 1200s;
fastcgi_connect_timeout 75s;
}
# TODO: http://example.com/foo/DIR/DIR/bar --> http://example.com/foo/DIR/bar
location ~* ^/(.+)$ {
if (-f $document_root/public/prov/$1.html) {
rewrite ^(.+)$ /public/prov/$1.html last;
break;
}
if (!-e $request_filename){
rewrite ^(.+)$ /load.php?request=$1 last;
break;
}
}
}
我希望你能帮助我,了解应该如何进行这种重定向。
PS 重定向后,必须按照PHP框架的最新指令进行处理。/load.php?request=$1
谢谢。
答案1
要使用 301 响应重定向,您需要使用rewrite...permanent
或return 301 ...
。
要删除 URI 的一部分,您需要捕获要删除部分之前和之后的 URI 部分 - 就像语句中的正则表达式一样location
。
例如:
rewrite ^(.*)/DIR/DIR(.*)$ $1/DIR$2 permanent;
看这个文件了解详情。