0

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?

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. Commented Nov 14, 2018 at 13:55

2 Answers 2

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'
);
0

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;    
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.