简体   繁体   中英

IIS 7.5 URL Rewrite Module - Rewrite Maps with variables

I have several pages on my site that have long and unreadable URLs that I'd like to be able shorten. The problem I'm having is appending query strings with variable values.

For example: "www.example.com/dir1/dir2/filename.php" shortens to "www.example.com/file". "www.example.com/dir1/dir2/filename.php?id=2" would be "www.example.com/file/2". "www.example.com/dir1/dir2/filename.php?id=2&alt=6" would be "www.example.com/file/2/6".

The values of 'id' and 'alt' are then used by our page to access information in our database, which determines the contents of the page. These values can change, and there is no set amount.

Right now I've got the first example working fine using the following rewrite rules:

<rewrite>
        <outboundRules>
            <remove name="OutboundRewriteUserFriendlyURL1" />
        </outboundRules>
        <rewriteMaps>
            <rewriteMap name="StaticRewrites">
                <add key="/file" value="/dir1/dir2/filename.php" />                    
            </rewriteMap>
        </rewriteMaps>
      <rules>
        <rule name="Rewrite Rule">
          <match url=".*" />
          <conditions>
            <add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
          </conditions>
          <action type="Rewrite" url="{C:1}" />
        </rule>
      </rules>
</rewrite>

But I haven't been able to find anything that would allow the URLs to contain variables. Everything I've seen has used static rewrites like my current solution is using, and I can't find anything about allowing arbitrary parameters.

EDIT:

Found a better solution that doesn't use a Rewrite Map. I had attempted a simliar rule previously, but due to the IIS setup on our testing environment it wasn't working as expected. This version should work for most people.

<rule name="Curricula View" stopProcessing="true">
    <match url="/file(?:/(\d+)(?:/(\d+))?)?" />
    <action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
</rule>

Using this rule I was able to avoid using a rewrite map altogether. I had attempted to use something like this originally, but due to the setup of our test environment it wouldn't work without some weird tweaks. This version should work in all normal environments.

<rule name="Curricula View" stopProcessing="true">
    <match url="/file(?:/(\d+)(?:/(\d+))?)?" />
    <action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
</rule>

EDIT: I was also able to get the original version with a rewrite map working for anyone interested.

<rule name="Rewrite Map with Variables" enabled="true">
    <match url="^(.+?)/?/(.*)$" />
    <conditions logicalGrouping="MatchAny" trackAllCaptures="false">
        <add input="{ProductMap:{R:1}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="/dir1/dir2/filename.php?id={C:0}&amp;other={R:2}" appendQueryString="true" />
</rule>

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