I have a seemingly easy requirement of IIS' URL Rewrite module that I can't seem to get working for a Single Page Web App. I have a root folder content and inside there is a folder dashboard. The URL uses dashboard at the root (i.e. content is omitted from the URL). If a URL matches any file, I'd like it to serve that, otherwise, I'd like it to serve index.html in the dashboard folder. The only complication is that dashboard is inside the content folder in the filesystem but content doesn't exist in the URL.
Here's my folder structure:
/content/dashboard/foo.txt --> serve this for URL: /dashboard/foo.txt
/content/dashboard/assets/image.jpg --> serve this for URL: /dashboard/assets/image.jpg
/content/dashboard/index.html --> serve this for any URL that starts with /dashboard/ if there isn't a file that matches
Here are my rewrite rules:
<rules>
<rule name="dashboard assets" stopProcessing="true">
<match url="^dashboard(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="content/{REQUEST_FILENAME}" matchType="IsFile" negate="false" />
</conditions>
<action type="Rewrite" url="content/dashboard{R:1}" />
</rule>
<rule name="dashboard app" stopProcessing="true">
<match url="^dashboard(.*)$" />
<action type="Rewrite" url="content/dashboard/index.html" />
</rule>
</rules>
Even though I have the dashboard assets conditions above, it never seems to use it and always serves index.html. How can I correct this?
{REQUEST_FILENAME} returns the full local directory path for the file requested. e.g. /content/dashboard/assets/image.jpg variable {REQUEST_FILENAME} is actually C:\inetpub\wwwroot\content\dashboard\assets\image.jpg
Remove the content/ from your condition input and it should work fine.
Related
If the user types the virtual path wrong such as test.com/user instead of test.com/User. IIS will return both /user and /User. I am lost on how to prevent this. The result i would like would be for it to return everything under /User regardless if typed /user.
*Note: In terms of this example I only have /User listed in IIS.
I have url rewrite installed in my iis, but i am having no luck
According to your description, I suggest you could try to use below url rewrite rule to achieve your requirement.
Details Url rewrite rule as below:
</rule>
<rule name="Prevent" stopProcessing="true">
<match url=".*" />
<conditions >
<add input="{URL}" pattern="user" />
</conditions>
<action type="AbortRequest" />
</rule>
I want my domain homepage to be redirected to one of my mvc directories
this is the rule i am using
<rule name="Home" stopProcessing="true">
<match url="^/*$" />
<action type="Rewrite" url="/test/" logRewrittenUrl="true" />
</rule>
Same rule is working if i put redirect. But rewrite is not working.
Other URLS should be served normally. Only home page should be rewritten
Can we achieve this without ARR?
//www.example.com should be rewritten to //www.example.com/test/
www.example.com/buy should be served as it is.
Your regex is incorrect. Correct rule is:
<rule name="Home" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/test/" logRewrittenUrl="true" />
</rule>
And this rule will work without ARR because you rewriting to a subfolder in the same website. If you want to rewrite to the different website in IIS, then you need ARR.
I am trying to redirect from my old ISP to my azure web site. I have set up the necessary DNS records. As I am using sub-folders on my Web App I need to set up redirection rules in the root web.config file to point from the domain name to the correct sub-folder of the main site.
It works perfectly when I have a redirect rule.
<rule name="Works" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
However when I change to a Rewrite it fails. I really want a rewrite as I don't want the user to see the change of url in the browser.
<rule name="Fails" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Rewrite" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
I get the following message:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
What am I doing wrong?
Your rewrite action should be a relative path, see the documentation:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rewrite_action
A substitution string must always specify the URL path
I have a website configured on IIS and multiple domains configured on my hosts file to point to my localhost.
My problem is that I need to redirect all the requests from:
http://domain.com/folder/ to http://domain.com/
So a request for
http://domain.com/folder/test/image.jpeg should be transformed into:
http://domain.com/test/image.jpeg
I can't change the files because I'm trying to emulate a cdn behavior.
Can anyone help?
Thanks
Joao
Using the rewrite module, you can go with:
<rule name="skip folder" stopProcessing="true">
<match url="^folder/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
The default redirect is a permanent (301).
If you want to keep the url http://domain.com/folder/test/image.jpeg but display the content http://domain.com/test/image.jpeg, then a rewrite has to be used:
<rule name="skip folder" stopProcessing="true">
<match url="^folder/(.*)$" />
<action type="Rewrite" url="{R:1}" />
</rule>
I have the following rule:
<rewrite>
<rules>
<rule name="FlowURLs">
<match url="^flows/[_0-9a-zA-Z-]+/[_0-9a-zA-Z-]+/([._0-9a-zA-Z-]+)" />
<action type="Rewrite" url="music.html?flow={R:1}" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="^.*\.(ashx|axd|css|gif|png|jpg|jpeg|js|flv|f4v|html)$" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
What I am trying to do is if a url is incoming something like the following:
http://localhost/flows/t/twit_nobone/twit_nobone1354334226132.mp3
I want to rewrite it to:
http://localhost/music.html?id=twit_nobone1354334226132.mp3
The music.html resides in the root and it has a stylesheet, javascript and images. The problem I am seeing is that when it rewrites, no images, js or stylesheets are being displayed. When I inspect the resources, it looks like the links are now
http://localhost/flows/t/twit_nobone/twit_nobone1354334226132.mp3/demo.css
http://localhost/flows/t/twit_nobone/twit_nobone1354334226132.mp3/images/screenshot.png
instead of
http://localhost/demo.css
http://localhost/images/screenshot.png
What am I doing wrong? I also noticed that the url in the browser never changes to the rewritten one.
You have to possibilities how to solve this issue.
1: if you want to keep address like http://localhost/flows/t/twit_nobone/twit_nobone1354334226132.mp3
then make sure that any resources in music.html are linked absolutely so instead of test.css make it /path/to/test.css from root server. Then it will work.
2: if you prefer address like
http://localhost/music.html?id=twit_nobone1354334226132.mp3
EDIT
I just found out it is simple - change the action to:
<action type="Redirect" redirectType="Found" url="music.html?flow={R:1}" />