Home > Backend Development > PHP Tutorial > Detailed explanation of php pseudo-static (url rewrite mod_rewrite module rewriting)

Detailed explanation of php pseudo-static (url rewrite mod_rewrite module rewriting)

WBOY
Release: 2016-07-25 09:12:00
Original
1117 people have browsed it

mod_rewrite is a very powerful function of Apache, which can realize pseudo-static pages.

1. Check whether Apache supports mod_rewrite Check the environment configuration through the phpinfo() function provided by php, and use Ctrl+F to find "Loaded Modules", which lists all modules that have been enabled by apache2handler. If "mod_rewrite" is included, it is supported and no further settings are required.

If "mod_rewrite" is not turned on, open the httpd.conf file under your apache installation directory "/apache/conf/", find "LoadModule rewrite_module" through Ctrl+F, and delete the "#" sign in front of it. Can. If it is not found, go to the "LoadModule" area, add "LoadModule rewrite_module modules/mod_rewrite.so" in the last line (required to be an exclusive line), and then restart the apache server.

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

Open the httpd.conf file (where? In the CONF directory of the APACHE directory), open it with a text editor, and look for:

  1. Options FollowSymLinks
  2. AllowOverride All
Copy the code

and you’re done.

3. Create .htaccess file

If you are on the Windows platform, I really don’t know how to create the ".htaccess" file at first, because this file actually has no file name, just an extension. This file cannot be created through ordinary methods. Don’t worry, it will be done right away. Let me tell you three methods: The three methods all first create a text file of htaccess.txt (of course, you can name this text file whatever you want), and then there are three ways to rename this file: 1) Open it with Notepad, click File – Save As, enter ".htaccess" in the file name window, pay attention to the entire green part, which includes English quotes, and then click Save. 2) Enter the cmd command window, use cd to switch the folder where the htaccess.txt file was just created, then enter the command: rename htaccess.txt .htaccess, and then click the Enter key on the keyboard. 3) Connect the folder where htaccess.txt is located through ftp and rename it through ftp software.

4, rewrite rules

Create a new .htaccess file and create the content:

  1. RewriteEngine on #rewriteengine is the rewrite engine switch on is on off is off
  2. RewriteRule ([0-9]{1,})$ index.php?id=$1
Copy code

RewriteRule: RewriteRule is a rewriting rule that supports regular expressions. The above ([0-9]{1,}) refers to being composed of numbers. $ is the end mark, indicating that it ends with a number!

Implement pseudo-static page, rules:

  1. RewriteEngine On
  2. RewriteRule ^index.html$ index.php
  3. RewriteRule ^new-(d+).html$ newxx.php?uid=$1
Copy the code

to implement http://127.0.0.1/index.html and http://127.0.0.1/new-1.html



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template