) Link static through URLRewrite (PHP)

Source: Internet
Author: User
Through URLRewrite to achieve link static (PHP) we know that the search engine is very friendly to static pages, therefore, many websites generate static pages to facilitate crawlers to capture the content of their websites. However, sometimes some applications are not suitable for all static operations, such as Forum/post-bars systems with extremely large data changes. at this time, we can rewrite the URL to implement pseudo-static link conversion) achieve link static through URL Rewrite (PHP)
We know that the search engine is very friendly to static pages. Therefore, many websites generate static pages to facilitate crawlers to capture the content of their websites. But sometimes some applications are not suitable for all static, such as Forum/post-bars systems with extremely large data changes. at this time, we can rewrite the URL to achieve pseudo-static link, that is, the website uses static links externally, while the dynamic page URL is still used internally. Like a link like this: http://www.ci123.com/abc.php? Action = a & id = 1. we can rewrite it to the form of http://www.ci123.com/abc/a/1.html. This is one of the most important aspects of search engine optimization. it also has an additional benefit to enable a permanent link on the page, even if the internal link of the website system changes in the future, by appropriately changing the Rewrite rules, you can ensure that the original external URL is always valid.

The following describes two simple URL rewriting methods in Apache + PHP. The first method is applicable to users with server configuration permissions, and the second method is applicable to users who rent space, it also serves as an example of my recent learning experience.


1. for users with server configuration permissions, we recommend that you use the mod_rewrite module of Apache. Here we assume that the mod_rewrite module has been installed. Open the Apache configuration file, find the corresponding host, and add the following code:
RewriteEngine On
RewriteRule ^/abc/([a-z] +)/(%0-9%%%%%.html $/abc. php? Action = $1 & id = $2 [L] and then execute service httpdreload in the shell to let Apache reload the configuration. Now on the PHP page, we can write the link as abc/a/1.html. When Apache parses this URL, it will rewrite it into abc. php? Action = a & id = 1 and return the correct page. Using regular expressions, we can implement almost any form of link we want. the mod_rewrite module has exceptionally powerful functions. this is just a simple application.

2. there is generally no way for users to modify the Apache configuration. here is a work und. The principle is as follows: when passing parameters to access the PHP page, normally, it is obtained through the automatic global variable $ _ GET, such as the above link, on the page, you can use $ _ GET ['action'] and $ _ GET ['id'] to obtain the result. the URL cannot be rewritten. Now, in each page, require a url_rewrite.php file with the following code:

$ Filename = basename ($ _ SERVER ['script _ name']);

If (strtolower ($ filename) = "abc. php "){
If (! Empty ($ _ GET [id]) {
$ Id = intval ($ _ GET [id]);
$ Action = intval ($ _ GET [action]);
}
Else {
$ Nav = $ _ SERVER ["REQUEST_URI"];
$ Script = $ _ SERVER ["SCRIPT_NAME"];
$ Nav = ereg_replace ("^ $ script", "", urldecode ($ nav ));
$ Vars = explode ("/", $ nav );
$ Action = intval ($ vars [1]);
$ Id = intval ($ vars [2]);
}
}
In this way, $ action and $ id are also obtained. the link on the page can be written as abc. php/a/1 to access the corresponding page.
Note that this method is less efficient than the first method. The first method is implemented in the WEB server URL parsing process, which is implemented in the PHP page parsing process, the 2nd method is only a work UND, and it is not convenient or flexible to modify the link form.
You are welcome to discuss this together!

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.