URL重写规则-用于查询字符串值的子文件夹 [英] URL Rewrite rule - subfolder to query string value
问题描述
我正在尝试创建一条规则,该规则将从URL中获取子文件夹并将其转换为查询字符串值,例如:
I'm trying to create a rule the will take the subfolder from the URL and convert that to a query string value for example:
如果我导航到以下URL: http://www.example.com/myfolder
我想阅读 http://www.example.com/default.aspx?folder=myfolder
If I navigated to this URL: http://www.example.com/myfolder
I would like that to read http://www.example.com/default.aspx?folder=myfolder
这是我要做的事情
<rule name="Rewrite Language">
<match url="([a-z]{2})(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/default.aspx?code={R:2}" />
</rule>
,但这不会返回完整的子文件夹值.老实说,我已经从本网站的类似问题中偷了这本书,我必须承认我真的不知道这意味着什么!
but this doesn't return the full subfolder value. I'll be honest I've stole this from a similar issue from this site, and I must confess I really have no idea what it all means!
我可能以错误的方式处理此问题,我的问题是我不确定该子文件夹是什么,因为它是由一个随机的6个字符的字母数字值动态生成的.
I might be approaching this in the wrong way, my issue is that I can't be sure what the subfolder will be as this is generated dynamically from a random 6 character alphanumeric value.
任何帮助将不胜感激.
大卫
推荐答案
IIS管理器具有GUI/向导界面,用于创建规则,与手动将规则输入到web.config文件中相比,通常我发现它更快捷,更容易.值得签出: IIS Manager->选择您的站点/应用程序->URL重写->添加规则
.
The IIS Manager has a GUI / wizard interface for creating the rules which I usually find quicker and easier than entering the rule into the web.config file manually. Worth checking out: IIS Manager -> select your site / application -> URL Rewrite -> Add Rule(s)
.
我认为以下规则将为您解决问题:
I think the following rule will do the trick for you:
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="default.aspx?folder={R:1}" />
</rule>
基本上,匹配网址"是正则表达式,用于标识其中的一部分网址.在这种情况下,它将捕获一个包含一个或多个字符(/除外)的组,并在URL的末尾带有一个可选的/.然后它将URL重写为 default.aspx?folder =
,后跟匹配的值( {R:1}
指第一个捕获的组,该组将包含文件夹名称).
Basically, the "match url" is a regular expression that is used to identify a part of the URL. In this case, it captures a group containing one or more characters (except for a /), with an optional / at the end of the URL. It will then rewrite the url to default.aspx?folder=
followed by the value that was matched ({R:1}
refers to the first captured group, which will contain the folder name).
这将起作用,前提是您只有一个子文件夹名称(而不是嵌套文件夹).
This will work provided you only have a single subfolder name (not nested folders).
您还可以添加第二条规则,该规则在相反的方向上起作用,因此浏览到 http://www.example.com/default.aspx?folder=myfolder
会导致用户看到 http://www.example.com/myfolder
:
You could also add a second rule which works in the opposite direction, so browsing to http://www.example.com/default.aspx?folder=myfolder
would result in the user seeing http://www.example.com/myfolder
:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^default\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^folder=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
这篇关于URL重写规则-用于查询字符串值的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!