AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1025272
Accepted
R.M. Reza
R.M. Reza
Asked: 2020-07-15 22:46:13 +0800 CST2020-07-15 22:46:13 +0800 CST 2020-07-15 22:46:13 +0800 CST

Nginx 将 Wordpress 帖子 URL 从“帖子名称”重定向到“类别帖子名称”

  • 772

如何使用来自 Wordpress URL“帖子名称”的 Nginx 进行 301 重定向:

example.com/best-movies-2020/

到“类别帖子名称”:

example.com/movies/best-movies-2020/

我是一个 nginx 初学者。我在 Goolge 中搜索过,但没有找到确切的解决方案。我非常感谢我能得到的任何帮助。

redirect nginx wordpress 301-redirect
  • 1 1 个回答
  • 342 Views

1 个回答

  • Voted
  1. Best Answer
    R.M. Reza
    2020-07-18T03:45:53+08:002020-07-18T03:45:53+08:00

    暂时我不使用 Nginx 重定向,我使用这个:

    add_filter( '404_template', 'custom_redirect_to_category' );
    
    function custom_redirect_to_category($template) {
    
        if ( ! is_404() ){
            return $template;
        }
    
        global $wp_rewrite;
        global $wp_query;
    
        if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure ){
            return $template;
        }   
    
        if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) ){
            return $template;   
        }
    
        $permalink = get_permalink( $post->ID );
    
        wp_redirect( $permalink, 301 );
        exit;
    
    }
    
    • 0

相关问题

  • 301 将根域重定向到 Godaddy Windows 主机帐户上的 www 子域

  • 使用 IIS7 重定向 Godaddy 中的子域

  • apache重写以将文件夹分配给域

  • 如何让 Apache2 重定向到子目录

  • 如何强制我的网址始终以 www 开头?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve