首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除Nginx中重定向301的url重复子目录

删除Nginx中重定向301的url重复子目录
EN

Server Fault用户
提问于 2019-01-23 18:28:53
回答 1查看 528关注 0票数 3

我必须删除一个子目录,该子目录已经复制在我的站点的几个URL中,通过永久重定向301。

我需要在Nginx配置方面的帮助。

例如:http://example.com/foo/DIR/DIR/bar -> http://example.com/foo/DIR/bar

代码语言:javascript
复制
location ~ ^(.*)/DIR/DIR(.*)$ {
    rewrite ^(.*)$ /DIR/$1 last;
    break;
}

我当前的nginx服务器配置:

代码语言:javascript
复制
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;
        }
    }

}

我希望你能帮我,了解这种重定向应该怎么做。

在重定向之后,它必须按照PHP框架的最新指令进行处理。/load.php?request=$1

谢谢。

EN

回答 1

Server Fault用户

回答已采纳

发布于 2019-01-24 12:30:51

要使用301响应重定向,需要使用rewrite...permanentreturn 301 ...

要删除URI的一部分,需要在要删除的部分之前和之后捕获URI的这些部分--就像location语句中的正则表达式一样。

例如:

代码语言:javascript
复制
rewrite ^(.*)/DIR/DIR(.*)$ $1/DIR$2 permanent;

详情请参见本文件

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/950429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档