Home >Backend Development >PHP Tutorial >thinkphp URL rules, URL pseudo-static, URL routing, URL rewriting, URL generation (15)_PHP tutorial
This chapter: Detailed introduction to thinkphp URL rules, URL pseudo-static, URL routing, URL rewriting, and URL generation
1. URL rules
1. The default is case-sensitive
2. If we don’t want to be case sensitive, we can change the configuration file
'URL_CASE_INSENSITIVE'=>true,//url is not case sensitive
*If the module name is too long:
A. If the module is named UserGroupAction, the complex module (usually IndexAction)
Then the url to find the module must be written as
http://localhost/thinkphp4/index.php/user_group/index
B. If 'URL_CASE_INSENSITIVE'=>false (accessible in case of case sensitivity)
Then the url can also be written as
http://localhost/thinkphp4/index.php/UserGroup/index
2. URL pseudo-static (tp supports pseudo-static by default)
http://localhost/thinkphp4/index.php/UserGroup/index.xml
*By default, pdo, html, xml... are all supported. If you want to limit it, just add a configuration
'URL_HTML_SUFFIX'=>'html|shtml|xml',//Restrict pseudo-static suffix
3. URL routing
1. Start routing
To enable routing support in the configuration file
'URL_ROUTER_ON' => true, //Turn on routing
2. Use routing
1. Rule expression configuration routing
'URL_ROUTER_ON' => true, //Turn on routing
'URL_ROUTE_RULES' => array(
'my'=>'Index/index',//static address routing visit: http://localhost/thinkphp/index.php/my
':id/:num'=>'Index/index',/*Write the numerical value after it, letters are also acceptable
Dynamic address routing visit: http://localhost/thinkphp/index.php/10/200
You can use the get method to pass the value in the module controller or get
echo $_GET['id'];
echo $_GET['num'];
*/
'my/:num'=>'Index/index', //Mixed dynamic and static address routing http://localhost/thinkphp/index.php/my/200
'year/:year/:month/:date'=>'Index/index',//Dynamic and static mixed address routing: http://localhost/thinkphp/index.php/year/2014/12/21
'year/:yeard/:monthd/:dated'=>'Index/index',//Dynamic and static mixed address routing --adding d means that the type can only be numbers
'my/:id$'=>'Index/index',// Add $ to indicate that the address can only be my/1000 and there can be no other content after it
);
2. Regular expression configuration routing
//http://localhost/thinkphp/index.php/year/2014/12/21
'/^year/(d{4})/(d{2})/(d{2})/'=>'Index/index?year=:1&month=:2&date=:3'
3. Notes:
1. The more complex routes are placed in front
'URL_ROUTE_RULES'=>array(
'my/:year/:month:/:day'=>'Index/day', *Complex routing is placed in the front, and it will not be executed if it is placed in the back
'my/:idd'=>'Index/index',
'my/:name'=>'Index/index',
)
2. You can use $ as an exact matching routing rule (all regular rules will be matched regardless of complexity)
'URL_ROUTE_RULES'=>array(
'my/:idd$'=>'Index/index',
'my/:name$'=>'Index/index',
'my/:year/:month:/:day$'=>'Index/day',
),
3. Use regular matching
'URL_ROUTE_RULES'=>array(
'/^my/(d+)$/'=>'Index/index?id=:1',
'/^my/(w+)$/'=>'Index/index?name=:1',
'/^my/(d{4})/(d{2})/(d{2})$/'=>'Index/day?year=:1&month=:2&day=:3',
),
4. URL rewriting
For example: http://localhost/thinkphp/index.php/Index/index.html/t/my ---- I don’t want index.php to appear
The following is the configuration process of Apache, you can refer to it:
1. The mod_rewrite.so module
is loaded in the httpd.conf configuration file.
2. AllowOverride None Change None to All
3. Make sure URL_MODEL is set to 2 (this step is omitted)
4. Save the following content as a .htaccess file and place it in the same directory as the entry file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
After restarting Apache, the original
You can access it by visiting
http://localhost/thinkphp/Index/index.html/t/my --Simplified URL address, better support for SEO
5. URL generation (detailed introduction in the manual)
public function url(){
echo U('Index/add'); // Generate the URL address of the add operation of the Index module
///thinkphp/index.php/index/add
}
Previous article http://qdxinbj8.2cto.com/index.php?m=content&c=content&a=public_preview&steps=1&catid=75&id=363637