PHP pseudo-static (URL rewrite mod_rewrite module rewrite) detailed

Source: Internet
Author: User

Mod_rewrite is a very powerful feature of Apache that can implement pseudo-static pages.

1, detect whether Apache support Mod_rewrite through the phpinfo () function provided by PHP to view the environment configuration, through Ctrl+f find "Loaded Modules", It lists all the modules that the Apache2handler has already opened, and if it includes "mod_rewrite", it is supported and no longer required to continue setting.

If "Mod_rewrite" is not turned on, open the directory under your Apache installation directory "/apache/conf/" under the httpd.conf file, through ctrl+f find to "LoadModule Rewrite_module", the previous " # "number to delete. If not found, go to the "LoadModule" area, add "LoadModule rewrite_module modules/mod_rewrite.so" (Required exclusive line) to the last line, and then restart the Apache server.

2, let Apache server support. htaccess How to enable the local Apache server to support ". htaccess"? Modify Apache's httpd.conf settings to allow Apache support. htaccess.

Open a. httpd.conf file (where?) In the Conf directory of the Apache directory), when opened with a text editor, look for:

    1. Options FollowSymLinks
    2. AllowOverride All
Copy Code

You can do it.

3. Create a. htaccess file

If it is under the Windows platform, I really do not know how to create a ". htaccess" file, because this file does not actually have a filename, only the extension, through the normal way is unable to build this file, don't worry, I'll tell you right away. Three ways: Three methods are first to create a htaccess.txt text file (of course, the name of the text file you can randomly take), and then there are three ways to rename the file: 1), open with Notepad, click File – Save As, in the File name window, enter ". Htaccess ", note that the entire green section, that is, contains the English quotation marks, and then click Save on the line. 2), go to cmd Command window, switch to the folder by CD when the Htaccess.txt file is just established, then enter the command: Rename Htaccess.txt. htaccess, then click the keyboard enter key. 3), through the FTP connection Htaccess.txt folder, through the FTP software rename.

4,rewrite rules

Create a new. htaccess file that creates the content:

    1. Rewriteengine on #rewriteengine为重写引擎开关on为开启off为关闭
    2. Rewriterule ([0-9]{1,}) $ index.php?id=$1
Copy Code

Rewriterule:rewriterule is a rewrite rule that supports regular expressions, the above ([0-9]{1,}) refers to numbers, $ is the end flag, and the description is the end of the number!

Implement pseudo-static pages, rules:

    1. Rewriteengine on
    2. Rewriterule ^index.html$ index.php
    3. Rewriterule ^new-(\d+). html$ newxx.php?uid=$1
Copy Code

Implement http://127.0.0.1/index.html and Http://127.0.0.1/new-1.html

  • 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.