IIS url-rewrite only where url has sub-folder - iis

I need to create a URL re-write/redirect rule in IIS that should only redirect requests that are for the home page of the site. Pages that are in sub-folders should not be redirected (we are rewriting to a different URL for mobile devices to a mobile specific site, but only for the home page. Sub-folders still need to go to the desktop version of the site.)
For example:
http://my.site.com or my.site.com should redirect
http://my.site.com/pages/page1.aspx and my.site.com/pages/page2.aspx should not
I have tried variations on just looking for a url that contains a '/', which work, but then if the url contains 'http://' it still matches the rule.

This rule will redirect only homepage to /pages/page1.aspx
<rules>
<rule name="Redirect home to /pages/page1.aspx" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/pages/page1.aspx" />
</rule>
</rules>

Related

IIS - Redirect http requests from url to another url in the same folder

I have an asp page as part of of web application hosted on IIS8. I would like to set up http redirection from this page to another aspx page within the same site.Isn't there a straightforward setting I can use in IIS? I've looked at http redirect and url rewrite with little luck.
Requests to
http://localhost/example.asp
needs to be redirected to
http://localhost/example.aspx
I figured it out. I was missing the URL Rewrite module.It can be downloaded here.Once you download, install the url rewrite module and it will look like this.Open the feature and add the rule.
Once you add a new rule it will show up in the web.config file under system.webserver tags or you can add it to the web.config file directly.
<system.webServer>
<rewrite>
<rules>
<rule name="asp_to_aspx" stopProcessing="true">
<match url="^example.asp$" />
<action type="Redirect" url="http://localhost/example.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>

redirect subdomains from one domain to another domain's subdomain in iis with url rewrite

I want to redirect subdomains that come to iis to subdomains in localhost:port that my dotnet core application is running.
Simply i want to redirect like this:
xyz.example.com => xyz.localhost:4000
john.example.com => john.localhost:4000
test.example.com => test.localhost:4000
example.com => localhost:4000 (no subdomain address redirects to localhost:4000)
How can I do this with url rewrite rules is iis?
Edit:
there are some solutions for one subdomain (like this question) but my question is more general to redirect all subdomain from one domain to corresponding subdomain in another domain.
Here is the rule that performs redirect you are asking for:
<rewrite>
<rules>
<rule name="Test Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((.+.)?)example.com" />
</conditions>
<action type="Redirect" url="http://{C:1}localhost:4000{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
It's pretty straightforward:
<match url=".*" /> matches any URL. Whether requested URL is in example.com domain is checked in later condition. match section is useless for our case because it operates with the URL part without a hostname (after first '/').
Condition <add input="{HTTP_HOST}" pattern="^((.+.)?)example.com" /> matches domains like xyz.example.com or example.com. If condition is matched, capture group {C:1} will contain xyz. for xyz.example.com and empty string for example.com, which will be used further.
<action type="Redirect" url="http://{C:1}localhost:4000{REQUEST_URI}" appendQueryString="false" />
The result URL is built as http://{C:1}localhost:4000{REQUEST_URI}. {C:1} will hold subdomain name as said above and {REQUEST_URI} contains URL part after hostname. We also set appendQueryString to false because query is a part of {REQUEST_URI}.
You should add this <rewrite> section to site web.config under <system.webServer>. You could also configure it manually from IIS Configuration Manager. Here is a screenshot of such configuration:
Finally, here is a set of test that demonstrates how urls are changed:
[TestCase("http://xyz.example.com", "http://xyz.localhost:4000/")]
[TestCase("http://xyz.example.com/some/inner/path", "http://xyz.localhost:4000/some/inner/path")]
[TestCase("http://xyz.example.com/some/inner/path?param1=value1&param2=value2", "http://xyz.localhost:4000/some/inner/path?param1=value1&param2=value2")]
[TestCase("http://example.com", "http://localhost:4000/")]
[TestCase("http://example.com/some/inner/path", "http://localhost:4000/some/inner/path")]
[TestCase("http://example.com/some/inner/path?param1=value1&param2=value2", "http://localhost:4000/some/inner/path?param1=value1&param2=value2")]
public void CheckUrlRedirect(string originalUrl, string expectedRedirectUrl)
{
using (var httpClient = new HttpClient(new HttpClientHandler {AllowAutoRedirect = false}))
{
var response = httpClient.GetAsync(originalUrl).Result;
Assert.AreEqual(HttpStatusCode.MovedPermanently, response.StatusCode);
var redirectUrl = response.Headers.Location.ToString();
Assert.AreEqual(expectedRedirectUrl, redirectUrl);
}
}

What's the proper method to tweak the URL in IIS or in web.config?

I am using Sitefinity CMS, one of my website sections for my pages is showing up in the URL and it makes the URLs look not very friendly, the section name is 'Footer' and the URL looks like this:
http://www.domain.com/footer/press-releases/first-press-release
How do I change/rewrite this URL in IIS or in web.config and remove the 'footer/' only so the URL looks like:
http://www.domain.com/press-releases/first-press-release
Thank you
In Sitefinity 6.3 and above you can edit the URL structure:
http://www.sitefinity.com/developer-network/forums/general-discussions-/sitefinity-6-3-released
This means, that even if the page is under the Footer group page, then you can remove the /footer/ from the URL of the page.
Just edit the Title and Properties and click on Change next to the URL.
I'd like to suggest you take a look at the IIS module URL Rewrite
http://www.iis.net/downloads/microsoft/url-rewrite
It would allow you to add rules allowing the 301 redirect you appear to need.
With the module installed you could add this to your web.config file:
<rewrite>
<rules>
<rule name="PressRelease" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^first-press-release$" />
<conditions>
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/press-releases/first-press-release" appendQueryString="false" />
</rule>
</rewrite>

IIS 7 Can you rewrite a rewrite

I am trying to get something like this working with iis 7 microsoft rewrite plugin.
if user requests...
/author/bob/ - show bobs author page
if user requests ...
/author/bob/rss - show authors rss page by rewriting it to
/author/bob/?rss
however i want to to do it in a generic way for urls that end with /rss e.g. for 2 path parts...
<rule name="rss 2 dir listing" stopProcessing="false">
<match url="^([^/]+)/([^/]+)/rss$" />
<action type="Rewrite" url="/{R:1}/{R:2}/?rss" appendQueryString="true" />
</rule>
so the above rule would have to go through to another rewrite rule i.e the one that maps to the author page.
but it looks like this now tries to may to a static directory i.e. the 404 error output is
Requested URL http://site.l:80/author/bob/?rss
Physical Path C:\Users\Workspace\site.l\author\bob\
so its looking for a static directory but you can see its rewritten in to ?rss and trying the url http://site.l:80/author/bob/?rss in the browser works
I am using iis with railo tomcat so it could be a connector issue
Here is the current author rule with the rss incorporated ideally i want to remove adding rss per rule and use generic rules above.
<rule name="author item" stopProcessing="false">
<match url="^author/([a-zA-Z0-9-]+)/(rss)?$" />
<action type="Rewrite" url="/index.cfm?event=AuthorController.getItem&name={R:1}&{R:2}" appendQueryString="true" />
</rule>

IIS: How can I redirect requests for one page to another?

I have written an app to replace a single page and need to redirect the requests for the old page to the new app.
In IIS, how would I redirect the request for
http://www.mysite.com/foo.aspx
to
http://www.mysite.com/bar/default.aspx
Thanks!
In your web.config do:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Foo_To_Bar" stopProcessing="true">
<match url="^foo.aspx" />
<action type="Redirect" url="/bar/default.aspx" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
</system.webServer>
If you don't want to write the redirects by hand, there is a tool for URL Rewrite and Redirects: http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module The installer says it is for 7.0 but it works with 8.5 as well.
What you need is URL rewriting. There are a number of options. Some are discussed in this question:
IIS URL Rewriting vs URL Routing

Resources