Implementing URL rewriting with a. htaccess file

Source: Internet
Author: User

Note: The first part comes from Http://www.cnblogs.com/wangkongming/archive/2012/11/13/2768251.html, the blogger's personal website concise and the poetry of me I I. Like to like like

The second part is written in my own practice.

A

Have you ever looked very refreshed on some website URLs, without any. php or. html suffixes to be surprised? That's how they do it.

1. Installing the Mod_rewrite module
Because the Apache server does not open the Mod_rewrite module by default, we have to start it manually. Open the Apache configuration file httpd.conf file, and locate the following line:
#LoadModule rewrite_module modules/mod_rewrite.so
The previous # number is an annotated identifier for the Apache configuration file, which means that the Apache server did not load the module. Remove the previous # number, save and restart the Apache server, this is the implementation of the load mod_rewrite this module. (Tip: Any configuration file for Apache httpd.conf files are to be restarted for Apache to take effect)

2. New. htaccess file
In the case of Windows system by default, no new file name can be created, it is recommended that you use the VIM editor to create a new one. By opening the Vim editor, you can enter a text and save it as a. htaccess. Many friends are accustomed to the Windows system, so the new file is a headache. There is the file name must not be wrong, I wrote today when the file saved as. htacess, see no, less a C, how to debug all wrong, fortunately finally found this low-level error.

3. Configure the. htaccess file
The . htaccess file must be placed in the root directory of your project and not placed under another folder. Configure the format of the. htaccess file as follows:

      Explanation: Rewriteengine on: Start URL rewrite engine
     first look at the following configured. htaccess file instance:
<ifmodule Mod_rewrite.c>
    rewriteengine on
    rewriterule ^index\.html$/index.php [L]
    Rewriterule ^shop/(. +)/$ shop/shop.php?providerid=$1 [L]
</ifmodule>

Analysis: Two rules were written on the above. htaccess file. When you enter index.html in the browser, the index.php file is called in the background. The symbol "^" is preceded by the character, believe that familiar with the regular expression of friends will not be unfamiliar, the symbol "$" is preceded by the character end. The symbol L means stop processing the next rule. The last one when we enter shop/2322 in the browser, the shop.php file is called in the background and passed the parameter provider=2322.
Configure the rules, you follow that one to write, remember to add an L symbol after each line.

Write one more:

http://www.yourdomin.com/category.php?id=12 wanted to rewrite it into http://www.yourdomin.com/category/12 .

  Rewriterule ^category/([0-9]+) $ category.php?id=$1

If there are multiple pages of articles such as: http://www.yourdomin.com/category.php?id=12&page=3 rewritten into HTTP://WWW.YOURDOMIN.COM/CATEGORY/12/3

  Rewriterule ^category/([0-9]+)/([0-9]+) $ category.php?id=$1&page=$2

If you want to upgrade your site and ensure that old links are available, you can also do so through the Mod_rewrite module. You just have to write a rule where the page is permanently moved. Examples are as follows:

  Rewriterule ^oldpage.html$ newpage.html [r=301]

Record it and use it later.

"Two" in fact, I want the effect is like his personal site as http://www.wangkongming.cn/category/poetry/link to the address so display//redirect link without suffix
rewriterule ^ (blog) ([\w/]*) \. ([a-za-z]+) $/$1$2 [r=302]

() parentheses correspond to the contents of the $302 Temporary redirection301 Permanent Redirection/$1$2 This/can remove the system added to the link on the address ... That's probably what it means.
The effect of the above sentence is:http://www.wangkongming.cn/category/index.htmlLink Redirection toHttp://www.wangkongming.cn/category/index----------------------------------------------------------------------------------------------------------- ---------------------------However, Http://www.wangkongming.cn/category/index is not accessible. Because the files in the folder are http://www.wangkongming.cn/category/index.html with the suffix after the static generation
So here's the sentence: //Make website address Category/index accessible to category/index.html rewriterule ^ (blog) ([\w/]*) $ $1$2\.html [qsa,l]The effect of the above sentence is to enable Http://www.wangkongming.cn/category/index to access http:// www.wangkongming.cn/category/index.html ---------------------------------------------------------------------------------------------------------- ---------------------------- I think the combination of these two words should be able to achieve what I want: //Cyclic redirection
Rewriteengine on
Rewriterule ^ (blog) ([\w/]*) \.html$/$1$2 [r=302,l]
Rewriterule ^ (blog)/([\d]*)/$1/$2\.html [qsa,l]

but the browser error: request address in cyclic redirection Well, then it didn't work out.
. htaccess placed in root directory
This site system is PHP-based

Implementing URL rewriting with a. htaccess file

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.