IIS URL Rewrite to not pass the .htm into the rewrite - iis

I have an IIS server with many domains multihomed. I have base rewrite rules for seo friendly URLS.
Example:
www.something.com/here-is-the-page
current converts to
www.something.com/index.htm?pagetitle=here-is-the-page
Problem is that one domain needs this rule to allow the URLs to read with .htm on the end.
Example:
www.something.com/here-is-the-page.htm
needs to convert to
www.something.com/index.htm?pagetitle=here-is-the-page
(note, just removed the .htm, although I still want the .htm to display in the location bar)...

Thanks everyone for all of the assistance...[sarcasm] below is the solution.
The key here is the MatchAll.
Look at the URL
Check to see if this is for www.something.com
Check to make sure there is not an actual file with the filename.htm
Check there is no directory with that name
If the file is sitemap.htm (which does exist, then do not process) This should be caught by the previous check for file, but just got added for overkill. Since this is a rewrite and not a redirect the .htm will remain displaying in the navigation bar.
<rule name="remove the htm" stopProcessing="true">
<match url="([_0-9a-z-]+)\.htm$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www.something.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="sitemap\.htm$" negate="true" />
</conditions>
<action type="Rewrite" url="/index.cfm?pagetitle={R:1}" />
</rule>

Related

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

Why are URL rewrites in Azure Web App virtual directory not being processed when root directory contains rewrites?

I have two virtual directories in an Azure Web App and confirmed that they're working properly as application directories.
The root of the Web App contains nothing except a web.config file with two rewrite rules that cause subdomains to point to subfolders like this:
<rule name="App1 Subdomain Redirect to SubFolder">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^app1.myservice\.net$" />
</conditions>
<action type="Rewrite" url="/app1/{R:0}" />
</rule>
<rule name="App2 Subdomain Redirect to SubFolder">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^app2.myservice\.net$" />
</conditions>
<action type="Rewrite" url="/app2/{R:0}" />
</rule>
(I understand this could be written more efficiently - I'm trying to remove as many variables as possible.)
In each of the virtual directories I have another web.config that contains a single rule to add an extension:
<rule name="Add Extension">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.ashx" />
</rule>
The problem I'm experiencing is that the "Add Extension" rule is never processed.
If I remove the rule from the virtual directory's config file and put it into the root's, all is well. (If I only do this for one virtual directory, it works fine for that directory while the other does not. If I do it for both, then all rules get processed.)
If I remove the "Add Extension" rule and try other rules the other rules do not get processed, which indicates to me this is not a problem specific to this particular rule.
If I remove all rules from the root's config, all rules in the virtual directories get processed.
My question is, has anyone else experienced this and what have they done as a workaround?
It's my understanding that rules should cascade from root to virtual directories in the same way other configuration settings do.
Searches have not been helpful.
Anand seems to have experienced the same problem on a non-Azure IIS site but no answer was proposed: http://forums.iis.net/t/1178488.aspx
Zielu1's question is somewhat similar but again did not receive a response: IIS URL rewrite in child virtual directory not redirecting
There are a fair number of other questions about rewrites, mostly about syntax and getting them to work in general, but few about virtual directory inheritance of rules.
Thanks for the help.
Try it matching full subdirectory paths
<rule name="Add Extension">
<match url="/MySubDirectory/.*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.ashx" />
</rule>

Hide page extension and table names in url

How do i hide my page extension for example
mysite.com/index.asp
how do i take off the extension .asp
and also how i hide tables field names in a URL. example is I'm displaying details of a record of 2 . so the link is going to be
mysite.com/details?record_id=2
how do i take off the record_1d=2 to make it
mysite.com/details
I'm working with asp classic.
thanks
You want this:
http://www.iis.net/downloads/microsoft/url-rewrite
Your rule would look like this:
<rewrite>
<rules>
<rule name="RewriteASP">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" />
</rule>
</rules>
</rewrite>
As far as I'm aware you can't rewrite the querystrings without retooling your code to pass data via HTTP posts.

URL Rewrite module for IIS7 Not Handling Rewrite Rule Correctly

I have the attached redirect and rewrite rules. Essentially, www.foo.com/vehicles/vehicle-detail.aspx?stockno=123456 is rewritten to www.foo.com/123456. This works as expected with the rules I have in place. Now, if I attempt to browse to a page that does not exist (i.e. www.foo.com/test.aspx) the rewrite rule qualifies and IIS attempts to forward the request to www.foo.com/vehicles/vehicle-detail.aspx (without the querystring). On the vehicle-detail.aspx page I have code to redirect to the homepage if there isnt a valid stockno in the querystring, so this is what is happening. I am not sure how to correct the rule(s), so that the condition does not qualify as true. I have attached rules and screenshots of Failed Request Tracing I enabled to watch the request/rewrite rule.
<rule name="Redirect StockNo" enabled="true" stopProcessing="true">
<match url="^Vehicles/Vehicle-Detail\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^stockno=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
<rule name="Rewrite StockNo" enabled="true" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Vehicles/Vehicle-Detail.aspx?stockno={R:1}" />
What you need is something more unique for the friendly pattern. If you know that it's always going to be digits, you can have:
<match url="^([\d]+)/?$" />
This will only match to www.foo.com/{digits} with an optional trailing slash.
Another option is to make the url something like www.foo.com/s/{stockno}/. With the /s/ you have something unique that you can confidently match. S is just an example that is short for stockno, so you can use what you want.

Sub-Domain URL Rewrite Issues

I have been scratching my head with this problem for at least two months. I have a classified ads section in our site which has SEO friendly URL's I want to rewrite these url's. I am not sure if the problem is related to the subdomain for each region. The URL may look like this:
These are working URL's:
http://milwaukee.storeboard.com/classifieds/for-sale/business/make-incredible-income-while-growing-your-local-economy/425
I want to make it goto this address on our server:
http://milwaukee.storeboard.com/classifieds/viewad.asp?ClassAdID=425
I am using the following code in the web.config file... but no matter what variations I try I cant get it to work..
<rule name="Classifieds Ad" stopProcessing="true">
<match url="^http://([^/]+)/classifieds/.*?(\d+)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="http://{R:1}/classifieds/viewad.asp?ClassAdID={R:2}" />
The only reason the first link works at this time is because I am using my 404 error page to process the request and then calling the page via MSXML2.ServerXMLHTTP.6.0 but this is unreliable and can timeout, especially during times of heavy usage. I want to remove this method and use the quick IIS URL Rewrite.
I would really appreciate any help you can provide
Try the below rule
<rule name="Classifieds Ad" stopProcessing="true">
<match url="^classifieds/.*?/(\d+)$" />
<conditions>
<add input="{HTTP_HOST}" type=”Pattern” pattern="^(.*)$">
</conditions>
<action type="Rewrite" url="{C:0}/classifieds/viewad.asp?ClassAdID={R:1}" />

Resources