URL Rewrite rule - subfolder to query string value

走远了吗. 提交于 2021-02-11 15:01:36

问题


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:

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

This is where I'm up to:

<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!

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.

Any help would be very much appreciated.

David


回答1:


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>

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).

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=([^=&amp;]+)$" />
    </conditions>
    <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>


来源:https://stackoverflow.com/questions/14367019/url-rewrite-rule-subfolder-to-query-string-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!