Php implements static url pseudo-static url. I usually use apache, nginx, htaccess, and so on. php is rarely used to achieve static url. it is troublesome to use php to achieve static url, in my opinion, php is not recommended for url static. 1. the principle of url static implementation 1. Dynamic u php is implemented through a program to implement url pseudo static.
Static url. I usually use apache, nginx, htaccess, and so on. php is rarely used to achieve static url. it is troublesome to use php to achieve static url, therefore, php is not recommended for url static.
I. principle of static url implementation
1. use a program to convert a dynamic url to a static url. the conversion method should be a unified interface. Put the static url in html, so we can see the static url through the page.
2. apache or others. load the rewrite module and add rewrite rules. In this way, when we click the static url in the page, we can turn to the correct url. Although PHP files are executed, static URLs are displayed in the address bar of the browser.
If you use php to achieve static url, you need to keep the static url in the address bar. This is troublesome. here is a simple example.
2. convert a dynamic url to a static url
$ 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", 'AC' => 'check ', 'name' => 'tank', 'page' => 5); echo"
"; // $ Rewrite = false, the following/test. php? is displayed? Class = User & act = check & name = tankecho url_rewrite ('test. php ', array ('class' => "User", 'Act' => 'check', 'name' => 'tank'); echo"
"; // $ Rewrite = true, the following/test is displayed. php/User/check/tankecho url_rewrite ('test', array ('class' => "User", 'Act '=> 'check ', 'name' => 'tank'); echo"
"; // $ Rewrite = true, the following/test/User/check/tankecho url_rewrite ('test', array ('class' =>" User ", 'AC' => 'check', 'name' => 'tank'), 'html'); echo"
"; // $ Rewrite = true, the following/test/User/check/tank.html?> is displayed. "User", 'Act '=> 'check', 'name' => 'tank');?> "> Test
? A simple method is provided to convert a dynamic url to a static url. the link generated on the page is as follows:
test?
If you click here, the error 404 will be reported, because the root cannot find the tank directory.This is also difficult, so we need to specify a php file for the directories and files that cannot be found. Apache, nginx, or htaccess should be used.
3. specify a unified portal
RewriteCond % {REQUEST_FILENAME }! -F // The File RewriteCond % {REQUEST_FILENAME} cannot be found }! -D // The Directory RewriteRule cannot be found./test3/index. php [L]
?
Whether you implement it in the form of. htaccess or in configuration files such as apache, you can. What do the above three sentences mean? if the directory cannot be found, go to the index. php file. if the file cannot be found, go to index. php.
This is done,When we access http: // localhost/test3/test. php/User/check/tankNow that you know the php file, it's easy.
The following operations are performed by rewriting http: // localhost/test3/test. php/User/check/tank.
4. index. php file, URL ing parsing class http://hudeyong926.iteye.com/admin/blogs/1113971
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]); // send a response to the php file in the request url, where it is test. php} if (class_exists ($ param_array [0]) {// Judge test. php file does the class $ obj = new $ param_array [0]; if (method_exists ($ obj, $ param_array [1]) {// check whether the User class has the check method $ obj-> $ param_array [1] ($ param_array [3]); // call this method, the result is (my name is tank) }}?>
?V. test. php file
Here, when we access the http: // localhost/test3/test. php/User/check/tank URL,
The result is as follows: My name is tank, and the address bar is still static.
Through php, we have completed a simple url rewriting process. In fact, we still use. htaccess or apache, but the rewrite rules are not in. htaccess or apache. If php is used completely to achieve static url, I personally feel that it is impossible.