douhu2370 2014-11-27 03:13
浏览 73
已采纳

URL重写WordPress页面查询字符串

I have a WordPress site with a custom page comparison that gets a string.

Now it takes:

mysite.com/phones/compare/?between=samsung-galaxy-note-3-vs-nokia-lumia-730

It needs to be like that:

mysite.com/phones/compare/samsung-galaxy-note-3-vs-nokia-lumia-730

I've tried adding code on my site's .htaccess that didn't worked for me and went to WordPress 404 page:

RewriteRule ^phones/compare/(.+?)/?$ phones/compare/?between=$1 [NC,L]

Also I need to redirect to the new search engine friendly URL too when user gets to mysite.com/phones/compare/?between= URL.

  • 写回答

2条回答 默认 最新

  • douye5949 2014-11-27 06:00
    关注

    You are getting a 404 page because WordPress internally doesn't understand how to handle the URL. So the solution you are looking for will be a combination of .htaccess (for the redirection) plus a rewrite rule so that WordPress can handle the URL

    Add the following to functions.php

    add_action( 'init', 'init_custom_rewrite' );
    
    function init_custom_rewrite() {
    
            add_rewrite_rule(        
                '^phones/compare/([^/]*)/?',        
                'index.php?page_id=XXX&between=$matches[1]',        
                'top' );
    }
    

    Replace XXX with the page_id of compare page, flush the rewrite rules by re-saving your permalink structure. Now add a rule to .htaccess for the redirection and that should work.

    EDIT

    Add the following code to functions.php to access between query var in the page

    add_filter('query_vars', 'my_query_vars', 10, 1);
    
    function my_query_vars($vars) {
        $vars[] = 'between'; 
        return $vars;
    }
    

    And you can then access it using get_query_var("between")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别