我正在尝试将不存在的woocommerce产品的url重定向到worpdress页面。
示例:
https://testname.com/product/abc 到https://testname.com/sample-page
这里没有发布为abc的产品。
此外,我在https://testname.com/product/def.
我试过了。htaccess,但似乎无法使用。本例中为htaccess。
提前谢谢。
我正在尝试将不存在的woocommerce产品的url重定向到worpdress页面。
示例:
https://testname.com/product/abc 到https://testname.com/sample-page
这里没有发布为abc的产品。
此外,我在https://testname.com/product/def.
我试过了。htaccess,但似乎无法使用。本例中为htaccess。
提前谢谢。
如果您更喜欢控制编码,可以使用
/product/
关键词url_to_postid() - 测试产品url是否存在,以在WordPress query is being setup
. 代码的优点。htaccess是相对独立于服务器的,例如服务器迁移、站点迁移等。以下代码放置在主题函数中。并在一个测试站点上被证明是有效的。
add_filter( \'request\', \'ws365986_check_request\' );
function ws365986_check_request( $query ) {
// var_dump($_SERVER[\'REQUEST_URI\']); // for debug
// only check for product url if /product/ is found in URL
if( isset( $_SERVER[\'REQUEST_URI\'] ) && preg_match( \'/\\/product\\//\', $_SERVER[\'REQUEST_URI\'], $matches ) ) {
// check if url product exist
if( empty( url_to_postid( $_SERVER[\'REQUEST_URI\'] ) ) ) {
// redirect if return is 0 (empty)
$url = home_url( \'/sample-page\' );
// wp_redirect( $url ); // because $url is prepared by internally, wp_redirect should be enough
wp_safe_redirect( $url );
exit(); // exit script after redirect
}
}
// default
return $query;
}
可以使用。htaccess文件中添加以下规则。htaccess:
重定向301https://testname.com/product/abc https://testname.com/sample-page
或
重定向301/产品/abc/示例页
显然,出于某种不合逻辑的原因,开发人员决定获取所有语言帖子的唯一方法是添加supress_filters=true 到WP\\u查询(而不是像这样language_code=all 选项)。无论如何,我的情况是,我需要获取所有语言的帖子,但也需要使用过滤器修改WP\\u查询。有没有办法强制将我的过滤器添加到查询中,即使supress_filters 设置为false?这是我需要添加的过滤器:add_filter( \'posts_where\', function($where, $wp_query) {