当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to change the url as specified in .htaccess file

问题描述:

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.";

}

网友答案:

You can use the $_SERVER['QUERY_STRING'] to get the /$1 part of the URL.

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

What about if I change this the code RewriteRule ^(.*)$ index.php?/$1 [L] to RewriteRule ^(.*)$ index.php/$1 [L] . I have been removed ?.

Then you can use the $_SERVER['PATH_INFO'] for that..

$pathInfo = $_SERVER['PATH_INFO'];

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

Look at $_SERVER['REQUEST_URI'].

分享给朋友:
您可能感兴趣的文章:
随机阅读: