How to change the url as specified in .htaccess file

时间:2015-07-08 15:55:42

标签: php url

I have wrote code in .htaccess file to change the standard URL to friendly URL.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

But unfortunately, I don't know how can I adjust the php code to compatible with the code that in .htaccess file

The following is just a simple code, because I don't know how can I do that.

$url = $_GET[0]; // I don't know how to get the unknown parameters

if(file_exists("controllers/". $url . ".php")){
    require_once ("controllers/". $url . ".php");
    if(class_exists($url)){
        $controller = new $url;
    }else{
        echo "Class deos not exists.";
    }

}else{
    echo "File deos not exists.";
}

2 个答案:

答案 0 :(得分:0)

Look at $_SERVER['REQUEST_URI'].

答案 1 :(得分:0)

您可以使用$_SERVER['QUERY_STRING']获取网址的/$1部分。

if ($_SERVER['QUERY_STRING'] === "/main") {
    echo "Main Page";
} elseif ($_SERVER['QUERY_STRING'] === "/sub") {
    echo "Sub Page";
} else {
    echo "File does not exists.";
}
  

如果我将代码RewriteRule ^(.*)$ index.php?/$1 [L]更改为RewriteRule ^(.*)$ index.php/$1 [L],该怎么办?我被删除了   ?

然后你可以使用$_SERVER['PATH_INFO']来实现..

$pathInfo = $_SERVER['PATH_INFO'];

if ($pathInfo === "/main") {
    echo "Main Page";
} elseif ($pathInfo === "/sub") {
    echo "Sub Page";
} else {
    echo "File does not exists.";
}