首页 > 解决方案 > If search contains "pc" search another site

问题描述

I want to check if the search contains a string "pc". If the search contains pc i want to redirect to another URL removing the term "pc" from the query and leaving the rest.

This should look like this:

www.somesite.com/?s=pc12121212 ----> sub.somesite.com/index.php?p=12121212

标签: javascriptphpjquerywordpress

解决方案


function CheckAndMove(url){
    const queries = url.split('?')[1]
    if(!queries) return; // leave if you have no queries at all

    const search = (new URLSearchParams(queries)).get('s') // extract the search query
    if(!search || !search.indexOf('pc')==-1) return // leave if you have no search query or it doesn't contain pc

    const id = search.replace('pc','') // remove the 'pc' part
    window.location = 'https://www.sub.somesite.com/index.php?p='+id;
}

推荐阅读