Use the RewriteRule-htaccess syntax in writing pseudo-static rules

Source: Internet
Author: User
Tags id3
: This article describes how to use the detailed syntax of RewriteRule-htaccess in writing pseudo-static rules. if you are interested in PHP tutorials, refer to it. I. Regular expression tutorial
Pseudo-static rule writing RewriteRule-htaccess Detailed syntax tutorial
To put it simply: pseudo-static is actually using PHP to parse the current address into another way to access the website! To learn how to write pseudo-static rules, you must understand some regular expressions. it doesn't matter. just follow the settings below.
I. Regular expression tutorial
There is a classic tutorial: Getting started with regular expressions for 30 minutes
This tutorial is indeed very simple. after reading it, there is no problem in writing some simple regular expressions. Regular expressions are a tool that requires long-term use, so you don't have to forget it for a period of time. so I read this tutorial every time. What is important after learning is that.
A simple list is as follows:
. All characters other than line breaks
\ W matches letters, numbers, underscores, and Chinese characters
\ S matches any blank space character
\ D matching number
\ B matches the start or end of a word
^ Match the start of a string
$ End of matching string
* Repeated zero or more times
+ Repeat once or more times
? Zero or one repetition
{N} repeated n times
{N,} repeat n times or more times
{N, m} repeat n to m times
When the application is replaced, the content matched in the first () is referenced with $1, and the matching content in the second () is referenced with $2 ......
The stuff in this () is called an atomic group.
Analyze the rewriting in the optimization htaccess of the discuz search engine.
RewriteRule ^ forum-([0-9] +)-([0-9] +) \. html $ forumdisplay. php? Fid = $1 & page = $2
First, join the user to access the discuz Forum through linuxidc.com/forum-2-3.html. then, filter through. htaccess to see if. htaccess is required to guide the user. if the list meets a series of RewriteCond conditions, rewrite them,
Discuz does not list RewriteCond, so it should all be overwritten.
So start transcription,
Forum-2-3.html this exactly matches the listed
^ Forum-([0-9] +)-([0-9] +) \. html $
Regular expression. And $1 is 2, $2 is 3,
So it is replaced by forumdisplay. php? Fid = 2 & page = 3 add the file directory specified by the previous RewriteBase, then take it to the forumdisplay. php? Fid = 2 & page = 3.
2. Examples of common. htaccess applications (some examples refer to the four examples to illustrate the rewrite rules for. htaccess files)
4.1 prevent leeching. if you want to access the url at the end of jpe jpg bmp png not from our website, let him see a picture of our website.
RewriteEngine OnRewriteCond % {HTTP_REFERER }! ^ Http: // (. + .)? Mysite.com/[NC] RewriteCond % {HTTP_REFERER }! ^ $ RewriteRule. *. (jpe? G | gif | bmp | png) $/images/nohotlink.jpg [L]
4.2 when a website is upgraded, only a specific IP address can be accessed. other users will see an upgrade page.
RewriteEngine onRewriteCond % {REQUEST_URI }! /Upgrade.html $ RewriteCond % {REMOTE_HOST }! ^ 24 \. 121 \. 202 \. 30
RewriteRule $ http://www.linuxidc.com/upgrade.html [R = 302, L]
4.3 change old domain names to new domain names
# Redirect from old domain to new domainRewriteEngine OnRewriteRule ^ (. *) $ http://www.yourdomain.com/?1=r=301,l]
III. Common examples
RewriteEngine On
RewriteRule index.html index. php
For example: http://www.yzzmf.com/index.html-> http://www.yzzmf.com/index.php
RewriteRule ^ test ([0-9] * example .html $ test. php? Id = $1
For example: http://www.yzzmf.com/test8.html> http://www.yzzmf.com/test.php? Id = 8
RewriteRule ^ cat-([0-9] +)-([0-9] +) \. html $ cat. php? Id1 = $1 & id2 = $2
For example: http://www.yzzmf.com/cat-1-3.html> http://www.yzzmf.com/cat.php? Id1 = 1 & id2 = 3
RewriteRule ^ cat-([a-zA-Z0-9 \-] *)-([0-9] +)-([0-9] +) \. html $ cat. php? Id0 = $1 & id1 = $2 & id2 = $3
For example: http://www.yzzmf.com/cat-zbc2ac-3-5.html> http://www.yzzmf.com/cat.php? Id0 = zbc2ac & id1 = 3 & id2 = 5
RewriteRule ^ cat1-([0-9] +)-([0-9] +)-([0-9] +) \. html $ cat1.php? Id1 = $1 & id2 = $2 & id3 = $3
For example: http://www.yzzmf.com/cat1-4-3-8.html> http://www.yzzmf.com/cat1.php? Id1 = 4 & id2 = 3 & id3 = 8
RewriteRule ^ cat ([0-9] *)/$ cat. php? Id1 = $1
For example: http://www.yzzmf.com/cat5/> http://www.yzzmf.com/cat.php? Id1 = 5
RewriteRule ^ catm ([0-9] *)/([0-9] *)/$ catm. php? Id1 = $1 & id2 = $2
For example: http://www.yzzmf.com/catm6/3/> http://www.yzzmf.com/catm.php? Id1 = 6 & id2 = 3
Hope to help you!

The above describes the use of the detailed syntax of the pseudo-static rule writing RewriteRule-htaccess, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.