Use the URL rewrite mechanism to implement static links

Source: Internet
Author: User
There are two main implementation methods:

Based on url rewrite
Download iis isapi rewrite (free of charge)
Based on path_info
Pass the URI address as a parameter: URL REWRITE

The simplest thing is to convert the URL Based on the URL rewriting (Rewrite) module in various WEB servers:
In this way, the program implementation will be almost unchanged? The link id = 234 is mapped to news/234.html, and looks like a static link from the outside. The Apache server has a module (non-default): mod_rewrite: powerful enough to write a book.

When I need? When id = 234 is mapped to news/234.html, you only need to set:
RewriteRule/news/(\ d +) \. html/news \. asp \? Id = $1 [N, I]
In this way, requests such as/news/234.html are mapped to/news. asp? Id = 234
When there is a request for/news/234.html: the web server will forward the actual request to/news. asp? Id = 234

In IIS, there are also corresponding REWRITE modules, such as isapi rewrite and iis rewrite. The syntax is based on regular expressions, so the configuration is almost the same as that of apache mod_rewrite:

For a simple application:
RewriteRule/news/(\ d +) \. html/news \. php \? Id = $1 [N, I]
So we map the http://www.chedong.com/news/234.html to the http://www.chedong.com/news/news.php? Id = 234

A more general expression that can map parameters of all dynamic pages is:
Put http://www.myhost.com/foo.php? A = A & B = B & C = C
The result is http://www.myhost.com/foo.php/a/a/ B/B /c/c/c.
Rewriterule (.*? \. Php )(\? [^/] *)? /([^/] *)/([^/] *) (. + ?)? $1 (? 2 $2 &:\?) $3 = $4? 5 $5: [N, I]

There are also some benefits through URL rewrite:

Hiding background implementation: This is very useful for background application platform migration: when migrating from ASP to Java, foreground users cannot feel the changes of background applications;
Simplified data validation: parameters such as (\ D +) can effectively control the number format and even digits;
For example, we need to extract the application from news. asp? Id = 234 migrate to news. php? When query is set to 234, the foreground performance can always be news/234.html. Separating the application from the foreground: This ensures the stability of the URL, while using mod_rewrite can even forward requests to other backend servers.

URL beautification Based on path_info
Another method of URL beautification is based on path_info:
Path_info is a CGI 1.1 standard. It is often found that many "/value_1/value_2" following CGI is the path_info parameter:
For example, http://www.chedong.com/phpman.php/man/ls,:$path_info = "/man/ls"

Path_info is a CGI standard, so PHP Servlet and so on are all supported. For example, the servlet contains the request. getpathinfo () method.
Note: getPathInfo () of/myapp/servlet/Hello/foo returns/foo, while/myapp/dir/hello. getPathInfo () of jsp/foo will return/hello. jsp. From here, you can also know that jsp is actually a Servlet PATH_INFO parameter. ASP does not support PATH_INFO,

The example of PATH_INFO-Based Parameter Parsing in PHP is as follows:
// Note: the parameters are separated by "/". The first parameter is null: The $ param1 $ param2 parameter is parsed from/param1/param2.
If (isset ($ _ SERVER ["PATH_INFO"]) {
List ($ nothing, $ param1, $ param2) = explode ('/', $ _ SERVER ["PATH_INFO"]);
}

How to hide an application: for example,. php, the extension:
Configure in APACHE as follows:

Forcetype application/X-httpd-PHP

How to make a static page more like: app_name/my/app.html
When the path_infoworkflow is complete, you can cut off the last 5“.html In the last step.
Note: In APACHE2, PATH_INFO is not allowed by default. You need to set
AcceptPathInfo on

PATH_INFO is usually the only option when you are not authorized to install and configure mod_rewrite for VM users.

OK. Now, you can see a webpage similar to http://www.example.com/article/234. can it be article/show. php? Id = 234 The dynamic web page generated by this php program. Many websites may have many static directories on the surface. In fact, it is very likely that the content is published using 1 or 2 programs. For example, many WIKIWIKI systems use this mechanism: the entire system is a simple wiki program, and the directory that appears is actually the Query Result of the application using the following address as the parameter.

Using the MOD_REWRITE/PATH_INFO + CACHE Server solution to transform the original Dynamic Release System can also greatly reduce the cost of upgrading the old system to the new content management system. It also facilitates indexing by search engines.
Appendix: How to Use PHP to support PATH_INFO on IIS
Php isapi mode installation memo: Just try to php-4.2.3-Win32

Unpack directory
==========
Php-4.2.3-Win32.zip c: \ php

PHP. INI initialization file
========================
Copy: c: \ php. ini-dist to c: \ winnt \ php. ini

Configuration File Association
================
Follow the instructions in install.txt to associate the configuration file

Running Library File
============
Copy c: \ php \ php4ts. dll to c: \ winnt \ system32 \ php4ts. dll

After this operation: php maps PATH_INFO to the physical path.
Warning: Unknown (C: \ CheDong \ Downloads \ ariadne \ www \ test. php \ path): failed to create stream: No such file or directory in Unknown on line 0

Warning: Unknown (): Failed opening 'C: \ CheDong \ Downloads \ ariadne \ www \ test. php \ path 'for declaration (include_path = '.; c: \ php4 \ pear ') in Unknown on line 0

Install the PATCH of ariadne
============================
Stop IIS service
Net stop iisadmin
Ftp://ftp.muze.nl/pub/ariadne/win/iis/php-4.2.3/php4isapi.dll
Overwrite the original c: \ php \ sapi \ php4isapi. dll

Note:
Ariadne is a content publishing system based on PATH_INFO,
In PHP 4.3.2, the CGI PATH_INFO in RC2 has been fixed. Install it as usual.

References:
URL Rewrite document:
Isapi rewrite documentation
Download iis isapi rewrite (free of charge)
Http://httpd.apache.org/docs/mod/mod_rewrite.html
Http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html

Search engine-friendly URL Design
Http://www.sitepoint.com/article/485
Maybe this URL is Articel. php? Id = 485

An open-source content management system based on path_info
Http://typo3.com/

Google PageRank algorithm description:
Http://pr.efactory.de/

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.