« Previous Next »

Answered Thread: Can I create a rewrite rule with a condition to check if the file exists?

Last post 11-04-2009 5:35 PM by rimbertr. 6 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (7 items)

Sort Posts:

  • 11-02-2009, 4:10 PM

    • rimbertr
    • Not Ranked
    • Joined on 11-02-2009, 3:52 PM
    • Posts 4

    Can I create a rewrite rule with a condition to check if the file exists?

    Hello all,

    I'm pretty new to the rewrite module so I hope you can help me.

     

    I have a current rule to rewrite:

    mysite.com/info/report/xxx

    to:

    mysite.com/info/report/xxx.htm

    where xxx is any combination of at least one alphabetic character.

     

    My rewrite rule is:

    <rule name="Rewrite info/report/xxx" stopProcessing="true">
      <match url="^info/report/([A-Za-z]+)" />
      <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="info/report/{R:1}.htm" />
    </rule>

     

    So far so good.  But there will be times where a user will make a request but the matching .htm file does not exist.  For example, if someone tries mysite.com/info/report/AAA but mysite.com/info/report/AAA.htm does not exist.  Instead of getting the site's 404 error, is there a way I can check first to see if the matching .htm file exists?  If it does, then go ahead with the Rewrite as above.  If it doesn't, then redirect to a special page, like info/report/noreport.htm.

    I've been searching for a few days now and I have not found a condition which involves checking to see if a file exists.  And I don't think IsFile is the same condition I'm talking about.


  • 11-03-2009, 11:08 AM In reply to

    Re: Can I create a rewrite rule with a condition to check if the file exists?

    No way to do this using URL Rewrite, but a custom 404 page may be able to do what you want.

    Jeff

    Look for Wrox's new book Professional IIS 7 in your local bookstore, or order now at Amazon.com
  • 11-03-2009, 12:51 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Re: Can I create a rewrite rule with a condition to check if the file exists?

    I thin you can check it by using this condition:

    <add input="{DOCUMENT_ROOT}/info/report/{R:1}.htm" matchType="IsFile" />

    http://ruslany.net
  • 11-03-2009, 1:46 PM In reply to

    • rimbertr
    • Not Ranked
    • Joined on 11-02-2009, 3:52 PM
    • Posts 4

    Re: Can I create a rewrite rule with a condition to check if the file exists?

     Thanks for the response, Jeff.  Isn't putting up a custom 404 page apply to the whole site, not just this one page?  From what I've found, the custom 404 page will show up any time the 404 error comes up.  I only want this custom 404 page to show if it doesn't exist in /info/report/.

  • 11-03-2009, 1:56 PM In reply to

    • rimbertr
    • Not Ranked
    • Joined on 11-02-2009, 3:52 PM
    • Posts 4

    Answered Re: Can I create a rewrite rule with a condition to check if the file exists?

     Thanks for the code, ruslany, but it didn't work as I hoped.

     I tried to add it to my current conditions (even tried it with negate="false") and it gives me a 500 error.

     I also tried the code as a rule before the existing rule and it wasn't doing what I wanted.

     

    Anyway, I'm now just creating an ASP page to check for the file and redirect to the appropriate URLs because I'm not sure it can be done with just rewrites.

  • 11-03-2009, 3:04 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 670

    Answered Re: Can I create a rewrite rule with a condition to check if the file exists?

    rimbertr:
    I tried to add it to my current conditions (even tried it with negate="false") and it gives me a 500 error.

    Which version of URL rewrite do you use? I just tried this rule and it worked fine:

                    <rule name="Check file exists">
                        <match url="([^.]+)" />
                        <conditions>
                            <add input="{DOCUMENT_ROOT}/{R:1}.aspx" matchType="IsFile" />
                        </conditions>
                        <action type="Rewrite" url="{R:1}.aspx" />
                    </rule>

    http://ruslany.net
  • 11-04-2009, 5:35 PM In reply to

    • rimbertr
    • Not Ranked
    • Joined on 11-02-2009, 3:52 PM
    • Posts 4

    Re: Can I create a rewrite rule with a condition to check if the file exists?

     ruslany,

    I'm using iis7.

Page 1 of 1 (7 items)
Microsoft Communities