PHP检入函数以将非尾部斜杠URL重定向到尾部斜杠URL

编程入门 行业动态 更新时间:2024-10-26 12:30:35
本文介绍了PHP检入函数以将非尾部斜杠URL重定向到尾部斜杠URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个控制分页Wordpress内容并将编号的URL重定向到其父URL的功能。

i've a function that control paginated Wordpress content and redirect numbered URLs to its parent URL.

该功能运行良好,但我希望重定向301用于没有最终斜杠的带编号的URL,直接触发到斜杠URL。例如:

The function is working perfectly but i want that the redirect 301 for numbered URLs that don't have a final trailing slash, fires directly to the trailing slash URL. For example:

www.example/how-to-do-something/1111

应立即重定向到

www.example/how-to-do-something/

目前,重定向301可以正常运行,但可以传递 www.example/how-to-do-something ,然后传递给 www.example/how-to-do-something/ 。

At the moment, instead the redirect 301 is working but pass for www.example/how-to-do-something and then to www.example/how-to-do-something/.

但是, 同时 ,此检查不应使带有最后斜杠的数字URL无效,例如,已经结束了,例如:

But, at the same time, this check should not invalidate the numbered URLs with final trailing slash, that are already good, for example:

www.example/how-to-do-something/1111/ 完全重定向到 https://www.example。 com / how-to-do-something / 。

函数如下:

global $posts, $numpages; $request_uri = $_SERVER['REQUEST_URI']; $result = preg_match('%\/(\d)+(\/)?$%', $request_uri, $matches); $ordinal = $result ? intval($matches[1]) : FALSE; if(is_numeric($ordinal)) { // a numbered page was requested: validate it // look-ahead: initialises the global $numpages setup_postdata($posts[0]); // yes, hack $redirect_to = isset($ordinal) ? '/': (($ordinal > $numpages) ? "/$numpages/" : FALSE); if(is_string($redirect_to)) { // we got us a phantom $redirect_url = get_option('home') . preg_replace('%'.$matches[0].'%', $redirect_to, $request_uri); // redirect to it's parent 301 header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently'); header("Location: $redirect_url"); exit(); } }

我该如何实现 PHP检查 从非跟踪斜杠URL直接转换为结尾斜杠 而没有 调用htaccess规则,我必须强制结尾削减?谢谢您的耐心和时间。

How can i achieve this PHP check from non-trailing slash URL directly to trailing slash without invoke the htaccess rule that i have to force the trailing slash? Thanks for your patience and time.

推荐答案

再次查看您的代码,有些东西没有加起来:

Looking again at your code there are some things that don't add up:

  • 行 $ redirect_to = isset($ ordinal)? '/':((($ ordinal> $ numpages)? / $ numpages /:FALSE); 将始终返回'/'因为$ ordinal总是在if语句中设置。

  • The line $redirect_to = isset($ordinal) ? '/': (($ordinal > $numpages) ? "/$numpages/" : FALSE); will always return '/' because $ordinal is always set within the if statement.

    'home'选项是否返回带有斜杠的URL?确保您需要'trailingslashit'函数,即 trailingslashit(get_option('home'))

    Does the 'home' option return a url with a trailing slash? to make sure you need the 'trailingslashit' function, i.e., trailingslashit(get_option('home'))

    总的来说,我的做法会有所不同。这就是我会做的(可以随意更改以适应您的需求):

    Overall I would approach this a bit differently. This is what I would do (fill free to change it to your needs):

    $request_uri = $_SERVER['REQUEST_URI']; $uriParts = explode('/', trim($request_uri, '/')); $ordinal = array_pop($uriParts); if (is_numeric($ordinal)) { setup_postdata($posts[0]); $redirect_url = trailingslashit(get_option('home')) . implode('/', $uriParts) . '/'; header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently'); header("Location: $redirect_url"); exit(); }

    希望这会有所帮助。

  • 更多推荐

    PHP检入函数以将非尾部斜杠URL重定向到尾部斜杠URL

    本文发布于:2023-11-01 11:24:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1549211.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:斜杠   尾部   函数   重定向   PHP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!