【问题标题】:PHP redirect if url contains specific string如果 url 包含特定字符串,则 PHP 重定向
【发布时间】:2019-12-08 13:52:04
【问题描述】:

我有一个名为 content.php 的文档,它为从两个不同类别发布的内容列表提供服务。

所以在这两种情况下,catgory_1 列表和 _category_2 列表都是 content.php 的服务器

mysite.com/category_1/post/

mysite.com/category_2/post/

那么,我需要根据 catgory_ 将用户重定向到不同的页面吗?列表,但不影响该类别中的帖子。

简历:

如果您访问 mysite.com/category_1/post/ 或 mysite.com/category_2/post/(您会收到帖子)

但是:

如果您访问 mysite.com/category_1/(您会被重定向到第 1 页) 如果您访问 mysite.com/category_2/(您会被重定向到第 2 页)

【问题讨论】:

    标签: php wordpress redirect


    【解决方案1】:

    你可以做这样的事情。 首先使用$_SERVER["REQUEST_URI"] 获取您的网址路径。 然后获取category_之后的所有,如果category之后都是数字则重定向。

    $uri = $_SERVER["REQUEST_URI"];
    $getCategory = substr($uri, strpos($uri, "category_")+9);
    $getCategory = str_replace("/", "", $getCategory);
    
    if (is_numeric($getCategory)) {
        header('Location: http://www.example.com/1');
    }
    else{
        header('Location: http://www.example.com/2');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 2014-02-26
      • 2012-10-05
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多