-
-
//Convert url to static url - function url_rewrite($file, $params = array (), $html = "", $rewrite = true) {
-
- if ($rewrite) { //Do not rewrite during the development stage. During development, set $rewrite = false
- $url = ($file == 'index') ? '' : '/' . $file;
- if (! empty ( $params ) && is_array ( $params )) {
- $url .= '/' . implode ( '/', array_slice($params, 0 , 2));
- $param = array_slice($params , 2);
- foreach($param as $key => $value){
- $url .= '/' . $key . '/' . urlencode ( $value );
- }
- }
- if (! empty ( $html )) {
- $url .= '.' . $html;
- }
- } else {
- $url = ($file == 'index') ? '/' : '/' . $file;
-
- if (substr ( $url, - 4 ) != '.php' && $file != 'index') {
- $url .= '.php';
- }
-
- if (! empty ( $params ) && is_array ( $params )) {
- $url .= '?' . http_build_query ( $params );
- }
- }
- return $url;
- }
-
- echo url_rewrite ( 'test', array ('class' => "User", 'act' => 'check', 'name' => 'tank','page'=>5 ) );echo "
";
- //$rewrite = In the case of false, the display is as follows/test.php?class=User&act=check&name=tank
echo url_rewrite ( 'test.php', array ('class' => "User", 'act' => 'check', 'name' => 'tank' ) );echo " ";
- //When $rewrite = true, the display is as follows/test.php/ User/check/tank
-
- echo url_rewrite ( 'test', array ('class' => "User", 'act' => 'check', 'name' => 'tank' ) );echo "
";
- //When $rewrite = true, the following is displayed/test/User/check/tank
echo url_rewrite ( 'test', array ('class ' => "User", 'act' => 'check', 'name' => 'tank' ), 'html' );echo " ";
- //$rewrite = If true, the display is as follows /test/User/check/tank.html
- ?>
-
Copy code
"User",'act'=>'check','name '=>'tank'));?>">test
The above converts the dynamic URL into a static URL, and the link will be generated on the page as follows:
test
If you click directly at this time, a 404 error will definitely be reported, because it is impossible to find the tank directory.
You need to specify a php file for the directories and files that cannot be found. This requires the use of apache, nginx, or htaccess, etc.
Three, designate a unified entrance
-
- RewriteCond %{REQUEST_FILENAME} !-f //File not found
- RewriteCond %{REQUEST_FILENAME} !-d //Cannot find directory
- RewriteRule . /test3/index.php [L]
Copy code
Code description:
If the directory is not found, go to the index.php file. If the file is not found, also go to index.php.
When accessing http://localhost/test3/test.php/User/check/tank, it will go to index.php.
The following content is operated by rewriting http://localhost/test3/test.php/User/check/tank.
Four, index.php file
-
- $filename = $_SERVER['REQUEST_URI']; //Requested url
-
- /**Requested url, "/test3/test.php/User/check/tank"
- * test.php is the php file to go to
- * User is the class name
- * check is the method name in the class
- * tank is to be passed to Check parameters
- * by bbs.it-home.org
- */
-
- preg_match("/(w+.php)/" ,$filename,$match); //Find the php file name
-
- $array = explode('/',$filename); //Split the static url
-
- $key = array_keys($array,$match[0 ]); //Get the subscript Array corresponding to the file ( [0] => 2 )
- $file_array = array_slice($array,0,$key[0]+1); //Array ( [0] = > [1] => test3 [2] => test.php )
- $param_array = array_slice($array,$key[0]+1); //Array ( [0] => User [1 ] => check [2] => tank )
-
- $file_path = implode('/',$file_array);
-
- if($array[$key[0]] != "index.php"){
- include_once($array[$key[0]]); //Include the php file in the request url, here it is test.php
- }
-
- if(class_exists($param_array[0])){ //Judge Check if there is a class User in the file test.php
-
- $obj = new $param_array[0];
- if(method_exists($obj,$param_array[1])){ //Check if there is a class User There is no check method
- $obj->$param_array[1]($param_array[3]); //Call this method, the result is (my name is tank)
- }
- }
- ?>
Copy the code
5, test.php file
-
- class User {
- public function check($name){
- echo "My name is".$name;
- }
- }
- ?>
Copy code
At this point, when accessing http://localhost/test3/test.php/User/check/tank, no error will be reported.
|