PHP检入函数以将非尾部斜杠URL重定向到尾部斜杠URL [英] PHP check in a function to redirect a non-trailing slash URL to trailing slash URL

查看:225
本文介绍了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:

https://www.example.com/how-to-do-something/1111

应立即重定向到

https://www.example.com/how-to-do-something/

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

At the moment, instead the redirect 301 is working but pass for https://www.example.com/how-to-do-something and then to https://www.example.com/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:

https://www.example.com/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:


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

  1. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆