[英]PHP - load a file depending on the name of the last directory in URL
如何根据URL中最后一个目录的名称加载PHP文件
范例1网址: http : //www.example.com/en/auto/范例2网址: http : //www.example.com/en/auto/mercedes/
如果url中的最后一个目录是auto(如第一个url示例),则加载文件auto.php
如果网址中的最后一个目录是mercedes(如第二个网址示例中所示),则加载文件mercedes.php
除了上述解决方案,您还可以尝试以下操作:
$url = trim('http://www.example.com/en/auto', '/');
$loadUrl=substr($url, strrpos($url, '/')+1);
header("Location:".$loadUrl.".php");
正如Michael Berkowski指出的那样, .htaccess
是正确的方法,但是,您可以使用类似的东西来实现一个(非常) hack版本( 注:我不知道您打算如何“加载”文件并从哪里来,所以我在用include()
):
<?php
// Remove trailing forward slash
$str = rtrim('http://www.example.com/test/is/best/',"/");
// Simple match characters from back
$exp = preg_match("!/([a-zA-Z0-9]{1,})$!",$str,$match);
// $match[1] gives you: best
if(isset($match[1])) {
// $redirect gives you: best.php
if(is_file($redirect = $match[1].".php")) {
// Include the best.php
include($redirect);
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.