URL rewrite a truncated date pattern---the strong application of regular expressions

Source: Internet
Author: User
Tags format regular expression truncated
Date | regular

Recently busy their own blog program, naturally transferred to the blog in the long talk of the URL rewrite problem. One reason is in multi-user blog system of a face problem, originally want to take my csdn blog Http://blog.csdn.net/joshualang to say things, think or use my Space (http://www.tyoo.net) bar, Because this is my blog after the work of the go.

Like Http://www.tyoo.net/blog/joshualang without the necessary URL rewrite, will be http://www.tyoo.net/blog/default.aspx? Bloger=joshualang like the blog address, we should know that most netizens are considered to be built on the network of another home, real life will consider the address of home, traffic good or bad, the same network of home also need to have a well remember the number. The number is not too long (not very decent OH), behind the add a bunch of parameters afraid to let people slowly see and fear, to see the article is facing such a large pile of http://blog.tyoo.net/Articles/Default.aspx? Did bloger=joshualang&articleid=20070118234530 feel anything? And then look at a lot of blog programs are going to get an effect bar http://blog.tyoo.net/joshua/Articles/2007/01/18/everyone to see the benefits of this effect, which also led to the focus of this article!

Yes, to achieve one of our goals through such a very regular string.

A feasible URL should follow the following criteria to select:

Brief

• Easy typing.

• The structure of the site can be seen.

• "Abridged", allowing users to browse the site by removing part of the URL.

To this point I do not have to say more, in fact, are to highlight the simple, practical.

Note: It is necessary to look at the documentation for Scott Mitchell on the MSDN website http://www.microsoft.com/china/msdn/library/webservices/asp.net/ Urlrewriting.mspx?pf=true

There are experts here to rewrite the principle of the URL is clear enough, do not know can also download the source code of the top document to study.

In order to map efficiency (people's time is precious, the programmer's time is more), directly using the urlrewriter.net components, in fact, has not contacted the URL rewrite experience, basically get the source program looked at several times began on the road. The beginning of course not to ask what technical content, as long as you can rewrite the success already feel very OK, and then is in the process of rewriting to find out the problem there are new ideas, new discoveries ... So I have this article.

Let's start now. This time the main focus is just mentioned in the date pattern URL rewrite.

Http://blog.tyoo.net/Articles/2007/01/18/233030/joshualang.aspx This is the ultimate effect to be achieved here.


Basics: Urlrewriter.net components (or, of course, self-written), Understanding regular expressions

Parameters: ArticleID//article number [type: string length: (LIKE:YYYYMMDDHHMMSS)/meaningful and not repeated]

Bloger//Bo main user name [type: Beginning of string letter]

One problem encountered during URL rewriting is a 404 error when accessing a non-existent directory or file. The advice in this document on MSDN is to create the necessary folder directories and empty pages in the program directory, saying that trouble is really troublesome and thousands of of directories need to be built.

Since it is not easy to access non-existent directory files, then we do not access such a directory will not do. To access the files we already have (all my URLs point to a Default.aspx page under the root blog and then dynamically load the control group to generate a different View feature page) Of course this time to point to the page ~/default.aspx;

The following task is to pass the parameter, of course, is the URL pass value. That's why it's rewritten.

The main character is coming again: regular expression.

It's really awesome to use regular expressions here. http://blog.tyoo.net/joshualang/Articles/2007/01/18/Default.aspx directory you may generate URL rewrite rules such as the following:

<RewriterRule>
<lookfor>~/(w{6,16})/articles/(D{4})/(D{2})/(d{2})/default.aspx</lookfor>
<sendto>~/default.aspx? Bloger=$1&year=$2&month=$3&day=$4</sendto>
</RewriterRule>

The rules written in this way must be truncated with a 404 error due to the absence of the visited page. Because it will follow your directory tag down, if the construction of the directory will be large, and then look at the following code:

<RewriterRule>
<lookfor>~/(w{6,16})/articles/(D{4})/(D{2})/(D{2})/(D{6})/default.aspx</lookfor>
<sendto>~/default.aspx? Bloger=$1&year=$2&month=$3&day=$4&time=$5</sendto>
</RewriterRule>

There's a lot more time to go and no one will choose to build the catalog. Then make the most of the existing files to complete the task.

In fact, a lot of people can think of a file name instead of the directory structure. Think about it, isn't it? Of course, this has to have a certain understanding of regular expressions.

Well, let's see how it's done.

<RewriterRule>
<lookfor>~/(w{5,16})/articles/(D{4}) \/(d{2}) \/(d{2}) \/(d{6}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$2$3$4$5&bloger=$1</sendto>
</RewriterRule>

It is easy to see that I used the "\" to "/" escaped, and used in the file name inside the current structure is

Http://blog.tyoo.net/joshualang/2007/01/18.aspx

It's obvious that my ArticleID is a string based on the number of minutes and seconds, because it makes sense to insert data without having to think about repetition, and it's convenient to use time here for queries. $2$3$4$5 's 14-bit combination is my articleid. By publication date, the article number can be easily traced. And the last benefit is evident in the omission.

Now let's cut out the time section:

<RewriterRule>
<lookfor>~/(w{5,16})/articles/(D{4}) \/(d{2}) \/(d{2}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$2$3$4&bloger=$1</sendto>
</RewriterRule>

In turn, we can abridge the URL to such a pattern: http://blog.tyoo.net/joshualang/Articles/2007.aspx even http://blog.tyoo.net/joshualang/ Articles/default.aspx

is not very simple. But you have to pay attention to a problem: what is not to be able to achieve, there are many need to consider the following:

What's the difference between http://blog.tyoo.net/joshualang/Articles/2007.aspx and http://blog.tyoo.net/joshualang/Articles/2007/.aspx, After the above rules, the latter can be normal operation? No

The same: Http://blog.tyoo.net/joshualang/Articles/2007/01/08/.aspx is also not good. You also need to define other coping rules to achieve the same rewrite effect as they are.

Well, the effect is generally out; here is the complete rule code:

<!--Author:joshua Li (joshuasco@126.com) qq:245965348-->
<!--Rules for Blog Content displayer-->
<RewriterRule>
<lookfor>~/([a-za-z]w{5,16})/default.aspx</lookfor>
<sendto>~/default.aspx? Bloger=$1</sendto>
</RewriterRule>
<!--Rules for Article Lister-->
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(d{2}) \/(d{2}) \/(d{1,6}) \/([a-za-z]w{5,16}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2$3$4&bloger=$5</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(d{2}) \/(d{2}) \/(d{1,6) ((\/)?). Aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2$3$4</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(d{2}) \/(d{2}) \/([a-za-z]w{5,16}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2$3&bloger=$4</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(d{2}) \/(D{2}) ((\/)?). Aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2$3</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(d{2}) \/([a-za-z]w{5,16}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2&bloger=$3</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/(D{2}) ((\/)?). Aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1$2</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) \/([a-za-z]w{5,16}) .aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1&bloger=$2</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/(D{4}) ((\/)?). Aspx</lookfor>
<sendto>~/default.aspx? Articleid=$1</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/articles/([a-za-z]w{5,16}) .aspx</lookfor>
<sendto>~/default.aspx? Bloger=$1</sendto>
</RewriterRule>
<RewriterRule>
<LookFor>~/Articles/Default.aspx</LookFor>
<sendto>~/default.aspx? Articleid=-1</sendto>
</RewriterRule>
Description: In order to visit the user to delete the important. aspx extension, I will use the user's masterpiece for the file's virtual identity name.

The last url:http://blog.tyoo.net/articles/2007/01/18/015000/joshualang.aspx

At the same time, the maximum length of 6 digits will be set to be truncated and longer, even if some numbers are lost, it is easiest to find the closest list to the publication time.

After you remove the user name will not affect the use, or through the first detailed time format will soon get the list with the most of the articles you need. If the username is intact, it can be truncated to find the author's list of articles for a specific time period.

Also note: The format of the user name ([a-za-z]{6-16}) and the order in which the rule is validated.

Summarize:
The URL rewrite process through the rule will have a tight format as it would actually exist in this directory, but the frame structure is more convenient and flexible, resulting in a focus on functionality and user experience.
Come here for the time being. If you have any questions, welcome back to join the discussion. If there is a better way to rewrite it is very happy to inform me oh.



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.