使用 cleanURL 资产添加正斜杠停止工作

Adding a forward slash using cleanURL assets stop working

我正在为一个学校项目做一个网站,我正在研究使用 cleanURL,所以我遵循了这个教程 www.你 tube.com/watch?v=ZgCW2x5QCXo,当我输入 /studytips/home 时,它工作得很好,但如果我添加 / 或其他东西,它就会停止工作。 发生的事情的图片Without the /foo With the /foo 我将包含他的 class,要使用它我会执行以下操作:

if($url->segment(1)){
    $s1=($url->segment(1));
    if((file_exists("includes/$s1.php"))){
        include("includes/$s1.php");
    } else {
        include("includes/404.php");
    }               
}else include("includes/home.php");

我想到了它的 hadling 不确定它是否是最好的方法^

class simpleUrl{

    var $site_path;

    function __construct($site_path){
        $this->site_path= $this->removeSlash($site_path);

    }

    function __toString(){
        return $this->site_path;
    }
    private function removeSlash($string){
        if( $string[strlen($string)-1] == '/')
            $string =rtrim($string,'/');


        return $string;
    }

    function segment($segment){

        $url = str_replace($this->site_path,'' , $_SERVER['REQUEST_URI']);
        $url = explode('/',$url);

        if(isset($url[$segment]) )

            return $url[$segment];
        return $url;
    }

}

htaccess 文件:

 <IfModule mod_rewrite.c>   RewriteEngine On
        RewriteBase /copy/studytips/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/
</IfModule>

您可以在 .htaccess 中使用:

RewriteEngine On
RewriteBase /copy/studytips/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php/

有或没有 / 都有效,但无论如何 returns 中的 / 重写 url.