admin管理员组文章数量:1130349
I'm a newbie in Wordpress, so excuse me if I ask a noob question.
I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.
My single-deal.php fires a GET request to an API, and my processing will follow thereafter.
For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.
Here's what I've tried reading on multiple other questions:-
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'single-deal.php?pagename=single-deal&property_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'property_id';
return $query_vars;
}
I'm a newbie in Wordpress, so excuse me if I ask a noob question.
I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.
My single-deal.php fires a GET request to an API, and my processing will follow thereafter.
For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.
Here's what I've tried reading on multiple other questions:-
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'single-deal.php?pagename=single-deal&property_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'property_id';
return $query_vars;
}
Share
Improve this question
edited Dec 16, 2018 at 17:19
Praful Bagai
asked Dec 16, 2018 at 11:46
Praful BagaiPraful Bagai
1013 bronze badges
1 Answer
Reset to default 1All internal rewrite rules must point to index.php. This isn't a theme file, it's the main bootstrap file in the root of your WordPress install. So, your rule should look like:
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'index.php?pagename=single-deal&property_id=$matches[1]',
'top'
);
}
You can then assign the deal.php template to your single-deal page in the admin interface, or you can use the page_template filter if you want to apply conditional template loading, like for example only if your property_id query var is set.
I'm a newbie in Wordpress, so excuse me if I ask a noob question.
I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.
My single-deal.php fires a GET request to an API, and my processing will follow thereafter.
For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.
Here's what I've tried reading on multiple other questions:-
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'single-deal.php?pagename=single-deal&property_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'property_id';
return $query_vars;
}
I'm a newbie in Wordpress, so excuse me if I ask a noob question.
I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.
My single-deal.php fires a GET request to an API, and my processing will follow thereafter.
For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.
Here's what I've tried reading on multiple other questions:-
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'single-deal.php?pagename=single-deal&property_id=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
$query_vars[] = 'property_id';
return $query_vars;
}
Share
Improve this question
edited Dec 16, 2018 at 17:19
Praful Bagai
asked Dec 16, 2018 at 11:46
Praful BagaiPraful Bagai
1013 bronze badges
1 Answer
Reset to default 1All internal rewrite rules must point to index.php. This isn't a theme file, it's the main bootstrap file in the root of your WordPress install. So, your rule should look like:
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'properties/([0-9]+)/?$',
'index.php?pagename=single-deal&property_id=$matches[1]',
'top'
);
}
You can then assign the deal.php template to your single-deal page in the admin interface, or you can use the page_template filter if you want to apply conditional template loading, like for example only if your property_id query var is set.
本文标签: redirectCustom URL routes
版权声明:本文标题:redirect - Custom URL routes 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749093425a2314878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论