admin管理员组文章数量:1130349
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
2 Answers
Reset to default 0If 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;
}
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
2 Answers
Reset to default 0If 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;
}
本文标签: url rewritingURL rewrite problem in WordPress plugin
版权声明:本文标题:url rewriting - URL rewrite problem in WordPress plugin 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749181674a2328715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论