Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

iis - Show default document extension in URL

I've been given several requests to rewrite a URL in IIS. One of them includes having the .html page as part of the URL, but without the HTML tag. For example, www.foo.com/bar.html would be displayed as www.foo.com/bar instead. I have this rule to accomplish that and it seems to work ok.

               <rule name="Hide .html ext" enabled="true">
                <match url="^(.*)" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
                </conditions>
                <action type="Rewrite" url="{R:0}.html" />
            </rule>

The problem I'm having is that since index.html is a default document in IIS, so www.foo.com never shows index.html for my html rewrite rule to handle it (at least that is my assumption as to what is happening). This is the same if the page is something like www.foo.com/bar/index.html. They would want www.foo.com/bar to redirect to www.foo.com/bar/index in the URL.

Essentially is it possible to somehow force the default document to show? I've been attempting all sorts of strange scenarios using {PATH_INFO}, {URL}, and {PATH_TRANSLATED} to somehow attempt to catch any index.html page to rewrite it "correctly" in the URL.

Example attempt that I know is in the wrong direction:

               <rule name="Include /index" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{PATH_INFO}" pattern="(.+?(?=(index.html|/index.html|/index.html/)$)|^(index.html))" />
                    <add input="{URL}" pattern="(.+?(?=(index|/index|/index/)$)|^((index)|(/index/)))" negate="true" />
                </conditions>
                <action type="Redirect" url="/index" appendQueryString="false" redirectType="Permanent" />
            </rule>

I feel like I'm overthinking this and there's possibly a simpler solution that uses IIS URL rewrite.

question from:https://stackoverflow.com/questions/65852830/show-default-document-extension-in-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the default document function is enabled, the index cannot be displayed in the URL, including index.html.

If you want to display the index in the url unless manually enter it in the browser. There are some problems with URL rewriting. Even if the URI is empty, the request is sent to IIS, and what is received in IIS is the URL with farvicon.ico attached. In URL rewriting, it is not very accurate to determine whether the URI is empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...