PHP friendly URL implementation

Source: Internet
Author: User
For the implementation of PHP-friendly URLs, read the implementation of PHP-friendly URLs. the following code mainly describes the implementation of pseudo-static URLs. do you often see URLs on other websites like this? Invalid.

The following code mainly implements pseudo-static operations, which search engines like

Do you often see URLs of other sites like this?
Http://www.xxx.com/module/show/action/list/page/7
Or
The http://xx.com/module/show/action/show/id/8.shtml with an extension
Or
Http://xx.com/module/show/action/show/id/8? Word = ss & age = 11
This is the case.
Today, I will announce the implementation of this method and work out the simplest code independently.
The functions are as follows. they are not encapsulated into classes. They are mainly unnecessary and convenient to use functions.

/**
* Get friendly URL access
*
* @ Accesspublic
* @ Return array
*/
Function getQueryString (){
$ _ SGETS = explode ("/", substr ($ _ SERVER ['path _ info'], 1 ));
$ _ SLEN = count ($ _ SGETS );
$ _ SGET = $ _ GET;
For ($ I = 0; $ I <$ _ SLEN; $ I + = 2 ){
If (! Empty ($ _ SGETS [$ I]) &! Empty ($ _ SGETS [$ I + 1]) $ _ SGET [$ _ SGETS [$ I] = $ _ SGETS [$ I + 1];
}
$ _ SGET ['M'] =! Empty ($ _ SGET ['M']) & is_string ($ _ SGET ['M'])? Trim ($ _ SGET ['M']). 'action': 'indexaction ';
$ _ SGET ['A'] =! Empty ($ _ SGET ['A']) & is_string ($ _ SGET ['A'])? Trim ($ _ SGET ['A']): 'RUN ';
Return $ _ SGET;
}
/**
* Generate a URL
*
* @ Accesspublic
* @ Param array $ arr
* @ Return string
*/
Function setUrl ($ arr ){
Global $ Global;
$ QueryString = '';
If ($ Global ['urlmode'] = 2 ){
Foreach ($ arr as $ k =>$ v ){
$ QueryString. = $ k. '/'. $ v .'/';
}
}
$ QueryString. = $ Global ['urlsuffix '];
Return $ queryString;
}
?>
Easy to use
$ _ GET = getQueryString ();
?>
But this does not work, so it can only be implemented
Http://www.xxx.com/index.php/module/show/action/list/page/7.
There is an additional index. php in the middle. we need to remove it and rewrite it.
But some files do not want this, such as style images.
Create a. htaccess file

RewriteEngine on
RewriteCond $1! ^ (Index \. php | css | pics | themes | js | robots \. txt)
RewriteRule ^ (. *) $ index. php/$1 [L]
Now, OK. test it now.

$ _ GET = getQueryString ();
Print_r ($ _ GET );
?>

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.