3.1 route support Customization

Source: Internet
Author: User
Tags subdomain subdomain name
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about the importance of SEO. Official routes do not support custom URLs as you like; this problem is a big problem during cms project development. The problem persists.
Make modifications based on the official route plug-in to customize the route.
It is not very complete and can be fully compatible with root directory access. The suffix and level-2 Directory are not supported!
Which of the following experts can continue to modify and improve if they are interested. I made changes on version 3.1, But I have roughly understood that it is also common to make slight changes on version 3.2. No more code is posted.
First, modify the U method.
Final Result Display for example accessing the user Module reg method display result as http://ml.topcms.pw/ RegHere, you can customize the reg without configuring the server. Function U ($ url = '', $ vars ='', $ suffix = true, $ redirect = false, $ domain = true ){
// Parse the URL
$ Info = parse_url ($ url );
$ Url =! Empty ($ info ['path'])? $ Info ['path']: ACTION_NAME;
If (false! = Strpos ($ url, '@') {// resolve Domain Name
List ($ url, $ host) = explode ('@', $ info ['path'], 2 );
}
// Resolve the subdomain name
If (isset ($ host )){
$ Domain = $ host. (strpos ($ host ,'.')? '': Strstr ($ _ SERVER ['HTTP _ host'], '.');
} Elseif ($ domain === true ){
$ Domain = $ _ SERVER ['HTTP _ host'];
If (C ('app _ SUB_DOMAIN_DEPLOY ') {// enable sub-domain name deployment
$ Domain = 'localhost '? 'Localhost': 'www '. strstr ($ _ SERVER ['HTTP _ host'],'. ');
// 'Subdomain name' => array ('Project [/group] ');
Foreach (C ('app _ SUB_DOMAIN_RULES ') as $ key => $ rule ){
If (false === strpos ($ key, '*') & 0 === strpos ($ url, $ rule [0]) {
$ Domain = $ key. strstr ($ domain, '.'); // generate the corresponding subdomain name
$ Url = substr_replace ($ url, '', 0, strlen ($ rule [0]);
Break;
}
}
}
}

// Parse parameters
If (is_string ($ vars) {// aaa = 1 & bbb = 2 convert to an array
Parse_str ($ vars, $ vars );
} Elseif (! Is_array ($ vars )){
$ Vars = array ();
}
If (isset ($ info ['query']) {// merge parameters in the resolution address to vars
Parse_str ($ info ['query'], $ params );
$ Vars = array_merge ($ params, $ vars );
}

// URL assembly
$ Depr = C ('url _ PATHINFO_DEPR ');
If ($ url ){
If (0 === strpos ($ url, '/') {// define the route
$ Route = true;
$ Url = substr ($ url, 1 );
If ('/'! = $ Depr ){
$ Url = str_replace ('/', $ depr, $ url );
}
} Else {
If ('/'! = $ Depr) {// Security replacement
$ Url = str_replace ('/', $ depr, $ url );
}
// Resolve groups, modules, and operations
$ Url = trim ($ url, $ depr );
$ Path = explode ($ depr, $ url );
$ Var = array ();
$ Var [C ('var _ Action')] =! Empty ($ path )? Array_pop ($ path): ACTION_NAME;
$ Var [C ('var _ module')] =! Empty ($ path )? Array_pop ($ path): MODULE_NAME;
If (C ('url _ CASE_INSENSITIVE ')){
$ Var [C ('var _ module')] = parse_name ($ VAR [C ('var _ module')]);
}
If (! C ('app _ SUB_DOMAIN_DEPLOY ') & C ('app _ GROUP_LIST ')){
If (! Empty ($ path )){
$ Group = array_pop ($ path );
$ Var [C ('var _ group')] = $ GROUP;
} Else {
If (GROUP_NAME! = C ('default _ group ')){
$ Var [C ('var _ group')] = GROUP_NAME;
}
}
If (C ('url _ CASE_INSENSITIVE ') & isset ($ var [C ('var _ group')]) {
$ Var [C ('var _ group')] = strtolower ($ VAR [C ('var _ group')]);
}
}
}
}

If (C ('url _ model') = 0) {// normal mode URL Conversion
$ Url =/index. php .'? '. Http_build_query (array_reverse ($ var ));
If (! Empty ($ vars )){
$ Vars = urldecode (http_build_query ($ vars ));
$ Url. = '&'. $ vars;
}
} Else {// PATHINFO mode or URL-compatible mode
If (C ('url _ ROUTER_ON ') & (C ('url _ model') = 2) & ($ var [C ('var _ group')] = ''| $ var [C ('var _ group')] = 'home ')){
$ Url =/index. php .'? '. Http_build_query (array_reverse ($ var ));
If (! Empty ($ vars )){
$ Vars = urldecode (http_build_query ($ vars ));
$ Url. = '&'. $ vars;
}
If (is_array (C ('url _ ROUTE_RULES '))){
Foreach (C ('url _ ROUTE_RULES ') as $ route_key => $ route_val ){
Preg_match ('#'. $ route_val. '$ #', $ url, $ metches );
If ($ metches ){
$ Route_url = $ metches;
$ Rule = $ route_key;
}
}
If ($ route_url ){
$ Url_new = explode (':', $ rule );
$ U = $ url_new [0];
For ($ I = 1; $ I $ U. = $ route_url [$ I]. '/';
}
$ U = trim ($ u ,'/');
$ Url = str_replace ('/', $ depr, $ u );
}
}
} Else {
If (isset ($ route )){
$ Url =/index. php. '/'. rtrim ($ url, $ depr );
} Else {
$ Url =/index. php. '/'. implode ($ depr, array_reverse ($ var ));
}
If (! Empty ($ vars) {// Add Parameters
Foreach ($ vars as $ v => $ val ){
If (''! = Trim ($ val) $ url. = $ depr. $ v. $ depr. urlencode ($ val );
}
}
}
// Route Filtering

If ($ suffix ){
$ Suffix = true? C ('url _ HTML_SUFFIX '): $ suffix;
If ($ pos = strpos ($ suffix, '| ')){
$ Suffix = substr ($ suffix, 0, $ pos );
}
If ($ suffix &&'/'! = Substr ($ url,-1 )){
$ Url. = '.'. ltrim ($ suffix ,'.');
}
}
}
If ($ domain ){
$ Url = (is_ssl ()? 'Https: // ': 'http: //'). $ domain... '/'. $ url;
}

If ($ redirect) // jump directly to the URL
Redirect ($ url );
Else
Return $ url;
}
Next, modify the route behavior. Here, I directly add the file below. If you are interested, download it.
For the specific effect, you can view your project.
Http://ml.topcms.pw

CheckRouteBehavior.class.php.zip (2.72 KB download: 40 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.