[英]Removing trailing slash from URL using PHP
What I need: 我需要的:
example.com/webpage/
should redirect to example.com/webpage
example.com/webpage/
应该重定向到example.com/webpage
I am using: 我在用:
$path = $_SERVER['REQUEST_URI'];
if(substr($path, -1) == '/')
{
$path=rtrim( $path, '/\\' );
header("Location: $path");
}
The problem: 问题:
When someone requests the homepage (with or without trailing slash), the value of $path
becomes '/'
and it causes a loop. 当有人请求首页(带斜线或不带斜线)时,
$path
的值将变为'/'
,并导致循环。 Can anyone offer me a better solution? 谁能为我提供更好的解决方案?
Try 尝试
$path = $_SERVER['REQUEST_URI'];
$path = preg_replace('#/\s*$#', '', $path);
header("Location: $path");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.