$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - URL rewrite problem in WordPress plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - URL rewrite problem in WordPress plugin

matteradmin6PV0评论

I am having some trouble getting rewrite rules to work as I want in my a WordPress plugin.

I added a rewrite rule:

add_rewrite_rule('some_url','some_redirected_url', 'top');

The rule is written to .htaccess and the rule works as expected; when inspecting the $_SERVER variable I get the following:

$_SERVER['REDIRECT_URL']='some_redirected_url'

$_SERVER['REQUEST_URI']='some_url'

However, WordPress is parsing $_SERVER['REQUEST_URI'] for request arguments so the redirected request is not parsed.

In other words, if I go to http://myserver/some_url the request is not working, even if the redirect works. If I go directly to http://myserver/some_redirected_url everything works correctly.

How can I make WordPress parse the redirected URL?

I am having some trouble getting rewrite rules to work as I want in my a WordPress plugin.

I added a rewrite rule:

add_rewrite_rule('some_url','some_redirected_url', 'top');

The rule is written to .htaccess and the rule works as expected; when inspecting the $_SERVER variable I get the following:

$_SERVER['REDIRECT_URL']='some_redirected_url'

$_SERVER['REQUEST_URI']='some_url'

However, WordPress is parsing $_SERVER['REQUEST_URI'] for request arguments so the redirected request is not parsed.

In other words, if I go to http://myserver/some_url the request is not working, even if the redirect works. If I go directly to http://myserver/some_redirected_url everything works correctly.

How can I make WordPress parse the redirected URL?

Share Improve this question edited Nov 14, 2018 at 19:51 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 14, 2018 at 11:16 user1660218user1660218 31 bronze badge 2
  • 1 What is the redirected url? A WordPress page, post, archive? External rewrites are limited, you probably want an internal rule, which is an entirely different format and mechanism. – Milo Commented Nov 14, 2018 at 13:32
  • The redirected url is to a custom post type created using register_post_type(). So the redirected url is supposed to be redirected again, but this does not work. Normally, rewrite rules in Apache will rewrite the url until there are no more rules that match, but in this case it seems to stop after the first match. – user1660218 Commented Nov 14, 2018 at 13:55
Add a comment  | 

2 Answers 2

Reset to default 0

If you want a rewrite rule that loads a post type archive from a different URL, the simplest way is via an internal rewrite, which doesn't get inserted into .htaccess. Internal rewrites map URLs to query arguments, and must result in a successful main query:

add_rewrite_rule(
    'some_url/?$',
    'index.php?post_type=yourcpt',
    'top'
);

I found a way to solve my problem, but it is not very elegant at all.
Before Wordpress parses the request I make sure the parsed request is the same as the redirected by setting the $_SERVER['REQUEST_URI'] directly.
Then after Wordpress is finished parsing i reset $_SERVER['REQUEST_URI'] back to its original value.
Not pretty at all, but at least the hack works like I want. Still interested in an answer if anyone can tell me the "right way" to do this...

My "hack" below

    add_filter('do_parse_request', 'myplugin_doParseRequestFilter', 10, 3);
    add_filter('request', 'myplugin_doRequestFilter', 10, 1 );



    function myplugin_doParseRequestFilter($some_bool, $request, $vars){
            //called before Wordpress starts the parsing...     
            if(isset($_SERVER['REDIRECT_URL'])){
                if(strpos($_SERVER['REDIRECT_URL'],'/_my_page_prefix_/')){
                    global $resetUrl;
                    $resetUrl=$_SERVER['REQUEST_URI'];   
                    $_SERVER['REQUEST_URI']=$_SERVER['REDIRECT_URL'];    
                }    
            } 
            return $request;    
        }


    function myplugin_doRequestFilter($vars){
        //called when Wordpress is finished parsing... 
        if(isset($_SERVER['REDIRECT_URL'])){
            if(strpos($_SERVER['REDIRECT_URL'],'/_my_page_prefix_/')){   
                global $resetUrl;
                $_SERVER['REQUEST_URI']=$resetUrl;             
            }    
        }
        return $vars;    
    }
Post a comment

comment list (0)

  1. No comments so far