About the mod_rewrite module of Apache

Source: Internet
Author: User

Converting dynamic web page Link rewriting into static link is the safest and most stable search engine-oriented optimization method. If the network
The website is designed to be better indexed by search engines. In addition to user friendly
Friendly) design is also very important. The more content you enter the search engine page, the more likely you will find using different keywords. In Google's algorithm survey
The number of pages indexed by Google on the site actually has a certain impact on PageRank. Because Google highlights the relatively static parts of the entire network (dynamic web page index volume comparison)
Small), static webpages with relatively fixed link addresses are more suitable for Google indexing (no wonder many large websites can easily search for mailing list archiving and blog archived documents by date ), therefore, many
The article for URL Design Optimization for search engines (URI pretty) mentions that many uses a certain mechanism to convert dynamic web page parameters into static web pages:

For example, you can set:

Http://www.domain.com/index.php? Product = MP3 & Action = ls

Into: http://www.domain.com/product/mp3/action/ls.html

The procedure is as follows:

1. Make sure that the mod_rewrite module is loaded in Apache HTTP. conf (not loaded by default ).

2. Make sure that the. htaccess overload is allowed in the site directory in Apache configuration, that is:

AllowOverride all

Note that if the site directory is in the VM settings, do not change the location.

3. After the configuration file is modified, restart Apache.

4. Create a. htaccess file in the directory where URL rewriting is required. For example, to rewrite the entire site, create a. htaccess file under the root directory of the site. Assume that the root directory of our site is:

C:/www/htdocs/test/

The root directory of the server is

C:/www/htdocs/

Create a. htaccess file in the C:/www/htdocs/test/directory. The file content is as follows:

<Ifmodule mod_rewrite.c>

Rewriteengine on

Rewritebase/test/

Rewritecond % {request_filename }! -F

Rewritecond % {request_filename }! -D

Rewriterule./test/index. php [l]

</Ifmodule>

Of course, if the site directory is the root directory, the content of the. htaccess file is as follows:

<Ifmodule mod_rewrite.c>

Rewriteengine on

Rewritebase/

Rewritecond % {request_filename }! -F

Rewritecond % {request_filename }! -D

Rewriterule./index. php [l]

</Ifmodule>

The preceding rewrite rule indicates that the URL rewriting function is enabled. For URLs starting with "rewritebase", if no direct file is found, rewrite it to/index. php.

Suppose we have the following directory structure (note: the site root directory is C:/www/htdocs/test /)

C:/www/htdocs/test/

|-. Htaccess

|-Index. php

|

|-Info (this is a directory)

|-. Htaccess

|/-Index. php

|

/-Info2

|-Test.html

/-Info. php

The content of the C:/www/htdocs/test/. htaccess file is:

<Ifmodule mod_rewrite.c>

Rewriteengine on

Rewritebase/test/

Rewritecond % {request_filename }! -F

Rewritecond % {request_filename }! -D

Rewriterule./test/index. php [l]

</Ifmodule>

The content of C:/www/htdocs/test/INFO/. htaccess is:

<Ifmodule mod_rewrite.c>

Rewriteengine on

Rewritebase/test/INFO/

Rewritecond % {request_filename }! -F

Rewritecond % {request_filename }! -D

Rewriterule./INFO/index. php [l]

</Ifmodule>

The following rewrite result is returned:

Try to access: http://www.domain.com/test/aaa/bbb/ccc.html

Actual access:/test/index. php

Expected parameter:/test/AAA/BBB/ccc.html

Try to access: http://www.domain.com/test/info/bbb/ccc.html

Actual access:/test/INFO/index. php

Expected parameter:/test/INFO/BBB/ccc.html

Try to access: http://www.domain.com/test/info2/test.html

Actual access:/test/info2/test.html

Obtain the parameter n/.

Try to access: http://www.domain.com/test/info2/abcd.html

Actual access:/test/index. php

Expected parameter:/test/info2/abcd.html

In the above content, "actual access" is $ _ server ['php _ Self '], and "Get parameter"
It is actually $ _ server ['request _ URI ']. In this way, we can separate the parameters according to certain rules in the PHP script. For example

Http://www.domain.com/test/a/aa/ B /bb.html:

...

$ Urlpath = php_self lookup the '/' character in reverse direction, and then discard the following file name. (get:/test /)

$ Param = request_uri-$ urlpath; (obtain a/AA/B/bb.html)

$ Param = $ param-'.html '; (obtain a/AA/B/BB)

$ Params = use '/' to split $ Param. (An array is obtained)

Obtain $ A = 'a'; $ B = 'bb' by using the parameters required for loop synthesis ';

Use these parameters

...

Of course, you can also design other parameter rules. For example, if a page has a fixed parameter format, you can:

Http://www.domain.com/test/AA/BB.html

The first parameter must be $ A, and the second parameter must be $ B. The effect is the same. However, such a rule design cannot be written as follows:

Http://www.domain.com/test/BB/AA.html

That's a mess.

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.