2014-11-27 03:13
浏览 73


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

Now it takes:

It needs to be like that:

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 URL.

图片转代码服务由CSDN问答提供 功能建议

我有一个WordPress网站,其中包含一个字符串的自定义页面比较。 < p>现在需要:

它必须是这样的:  vs-nokia-lumia-730 

我尝试在我的网站上添加代码.htaccess但这对我没用,并转到WordPress 404页面:< / p>

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


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douye5949
    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() {
                '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.


    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")

    点赞 评论
  • doulan4939
    doulan4939 2014-11-27 03:38

    You need to add your rules before the wordpress rules. The one you have should work as long as it's before any of the wordpress rules, but in order to redirect a request with the query string you need this:

    RewriteCond %{THE_REQUEST} \ /+phones/compare/\?between=([^&\ ]+)
    RewriteRule ^ /phones/compare/%1? [L,R]

    Again, the rule that you have plus the one above need to be before all of your wordpress rules.

    点赞 评论