Rewriterule-htaccess Detailed Syntax Usage tutorial

Source: Internet
Author: User
Tags id3

http://www.3code.cn/rewriterule-htaccess%E8%AF%A6%E7%BB%86%E8%AF%AD%E6%B3%95%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/

First, regular expression tutorial

There is a classic tutorial: Regular expression 30-minute introductory tutorial, you can search for a bit.
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 ...

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 start the transliteration, forum-2-3.html this exactly matches the listed ^forum-([0-9]+)-([0-9]+) \.html$ regular expression. and $2, $3, so generation

Into the back, that is, forumdisplay.php?fid=2&page=3 plus the file directory specified in the previous rewritebase, then take him to the development 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.3code.cn/index.html-http://www.3code.cn/index.php

Rewriterule ^test ([0-9]*). html$ test.php?id=$1

For example: http://www.3code.cn/test8.html-http://www.3code.cn/test.php?id=8

Rewriterule ^cat-([0-9]+)-([0-9]+) \.html$ cat.php?id1=$1&id2=$2

For example: http://www.3code.cn/cat-1-3.html-http://www.3code.cn/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.3code.cn/cat-zbc2ac-3-5.html-http://www.3code.cn/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.3code.cn/cat1-4-3-8.html-http://www.3code.cn/cat1.php?id1=4&id2=3&id3=8

Rewriterule ^cat ([0-9]*)/$ cat.php?id1=$1

For example: http://www.3code.cn/cat5/-http://www.3code.cn/cat.php?id1=5

Rewriterule ^CATM ([0-9]*)/([0-9]*)/$ catm.php?id1=$1&id2=$2

For example: http://www.3code.cn/catm6/3/-http://www.3code.cn/catm.php?id1=6&id2=3

Hope to help everyone!

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.