PHP pseudo static rule notation rewriterule-htaccess verbose syntax usage

Source: Internet
Author: User
Tags id3 time interval

First, regular expression tutorial

Pseudo static rule notation rewriterule-htaccess detailed syntax use tutorial share

Simply put: pseudo-static is actually using PHP to parse the current address into another way to visit the site! To learn the writing of pseudo-static rules, you have to understand a little regular, it doesn't matter, just follow the set below.


First, regular expression tutorial

There is a classic tutorial:Regular Expression 30-minute introductory teachingRide
This tutorial is really simple, and after reading basically write some simple regular there is no problem. Regular is a long-term use of the tool, not to forget the time interval, so I read this tutorial every time. In fact, after learning the important thing is a little content.

Here's a quick list:

All characters except line breaks
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times

When the substitution is applied, the match in the first () is followed by a reference, and the match in the second () is applied with the app ...
This one () is called an atomic group.
Analysis of Discuz SEO htaccess inside the rewrite.

Rewriterule ^forum-([0-9]+)-([0-9]+) \.html$ forumdisplay.php?fid=$1&page=$2

First join the user through linuxidc.com/forum-2-3.html access to the Discuz forum, then first through the. htaccess filter to see if it is needed. htaccess Guide the user if the list of Rewritecond Condition then rewrite it,

Discuz is not listed Rewritecond so it should all be rewritten.
So we started the transliteration,
Forum-2-3.html this exactly matches the list of
^forum-([0-9]+)-([0-9]+) \.html$
The regular expression. and $2, $3,

So substituting the following, that is, forumdisplay.php?fid=2&page=3 plus the previous rewritebase specified file directory, then take him to the development of the directory forumdisplay.php?fid=2&page=3.

Ii. Common. htaccess application examples (some examples are quoted from four examples of actual combat.) htaccess file rewrite rules)

4.1 Prevent hotlinking, if come to access jpe jpg bmp png End of URL user is not from our site, then let him see a picture of our site display.
Rewriteengine Onrewritecond%{http_referer}!^http://(. +.)? mysite.com/[Nc]rewritecond%{http_referer}!^ $RewriteRule. *. (jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L]

4.2 When the website is upgraded, only certain IP can be accessed, and 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 Turn the old domain name to the new domain
# 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]*). 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 everyone!

Reproduced in original: http://blog.sina.com.cn/s/blog_7fb6dadd0101ewpn.html

PHP pseudo static rule notation rewriterule-htaccess verbose syntax usage

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.