WordPress 404链接

发布于 2025-02-13 20:19:27 字数 462 浏览 0 评论 0原文

hii我想将用户重定向,如果页面没有找到home_url(404)。这可能吗?我的目标是在URL中发现的一些特殊词,例如产品,然后将用户发送到该页面,如果不包含我的URL,则可以将用户重定向到404页。这是代码示例

$redirect = false;
if( is_404()){
    $url = home_url( $wp->request );
    $path_to_check_for = '/product/';
        if( strpos($url, $path_to_check_for)  !== false ) {
            $redirect = true;
        }
        if($redirect == false){
            header("Location: ".home_url(404));
            exit;
        }

}

Hii I want to redirect users if page not found to home_url(404) like this. Is this possible? My goal is some special word like product if found in url then send user to that page and if not contain my url with product then redirect to 404 page. Here is code example

$redirect = false;
if( is_404()){
    $url = home_url( $wp->request );
    $path_to_check_for = '/product/';
        if( strpos($url, $path_to_check_for)  !== false ) {
            $redirect = true;
        }
        if($redirect == false){
            header("Location: ".home_url(404));
            exit;
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘑菇王子 2025-02-20 20:19:27
function custom_redirect_404() {
    if(is_404() && strpos($_SERVER["REQUEST_URI"], '/product/') === false ) {  
            wp_redirect( home_url(404));
        }
    }


add_action('wp_head', 'custom_redirect_404');

编辑:如果410页包含字符串'/product/'它将重定向到主页

function custom_redirect_404() {
    if(is_404() && strpos($_SERVER["REQUEST_URI"], '/product/') === false ) {  
            wp_redirect( home_url(404));
        }
    }


add_action('wp_head', 'custom_redirect_404');

Edited: If the 410 page contains the string '/product/' it redirects to homepage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文