简体   繁体   中英

IIS 7.0 Url Rewrite2 Rule and Regex Issues

I am using IIS.net's Url Rewrite program on my server to make rules for URL Rewrite. I have pages on my Asp.NET 4.0 Web Site such as Page.aspx, and News.aspx.

When I create a rule for Page.aspx ( http://www.foo.com/Page.aspx?PageTitle=Foo to http://www.foo.com/Foo ) It works great.

When I create a rule for News.aspx ( http://www.foo.com/News.aspx?NewsTitle=Foo to http://www.foo.com/Foo ) It works great too.

But when I create rules for both News.aspx and Page.aspx, it fails. Program looks rule order. If News's rule is at top, News.aspx Url rewritten, and Page.aspx Url rewrite fails. If Page's rule is at top, Page.aspx Url rewritten, and News.aspx Url rewrite fails.

I guess thhis problem occurs because of regex matching. Let me show you:

在此处输入图片说明

Rule for News.aspx is at top. So it works fine. But when I click Page.aspx, the RewriteUserFriendlyURL1's Pattern catch my URL, and tries to redirect it News.aspx (Shown in Action Url).

I hope my description is clear.

My question is: How can I avoid this problem? For example I can make a new page called Redirect.aspx, and can redirect all pages to here. I can make 1 rule for Redirect.aspx.

But I think there should be another way to solve this issue with configuring these rules.

Thank you.

The problem is that the regular expressions for RewriteUserFriendlyURL1 and RewriteUserFriendlyURL2 are exactly the same so in that case the order in which they are listed decides which rule gets applied first.

I understand what you are trying to accomplish but it can not work like that. It's impossible for the URL rewrite module to know if /Foo is a news article or a page. Your best option is probably to put the news under /news/<title> and keep /<title> for your regular pages. In that case you can use /news/ to make a difference between news and pages.

So you should keep RedirectUserFriendlyURL2 and RewriteUserFriendlyURL2 the same but change the action URL for RedirectUserFriendlyURL1 to /news/{C:1} and change the pattern for the RewriteUserFriendlyURL1 rule to ^news/([^/]+)/?$ (note: no slash between ^ and news ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM