我正在为一个学校项目做一个网站,我正在研究使用cleanURL,所以我遵循了这个教程www。你tube.com/watch?v=ZgCW2x5QCXo,当我把/studytips/家它工作得很好,但如果我添加一个/或一些东西它停止工作。没有/foo会发生什么加上/foo我将包括他的类,使用它我做以下的事情:
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");
我想出了处理方法,但不确定这是否是最好的方法^
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/$1
</IfModule>
您可以在您的.htaccess
中使用:
RewriteEngine On
RewriteBase /copy/studytips/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php/$1
可以使用或不使用/
,但在任何情况下都返回de重写url中的/
。