我必须通过永久重定向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
谢谢你。
要使用301响应进行重定向,你需要使用rewrite...permanent
或return 301 ...
。
要删除URI的一部分,你需要在要删除的部分之前和之后捕获URI的那些部分,这与location
语句中的正则表达式非常相似。
例如:
rewrite ^(.*)/DIR/DIR(.*)$ $1/DIR$2 permanent;
有关详细信息,请参见此文档。