IIS / MVC web.config rewrite - asp.net-mvc

I have two projects on my server on running at same ip.
a.company.com (MVC)
b.company.com (WEBAPI)
I want to redirect;
a.company.com/api/(.*) => **b.company.com/(.*)
Like that, but i want to keep b.company.com hostname is hidden. Its mean i want to rewrite it, not redirect.
Thanks.

You will need to have the URL Rewrite module 2.0 loaded, which is available via the platform installer. this will allow you to do server-side URL rewriting.
I can't say exactly what your rules should be but if you do it at the site level you will get somthing like this in your web.config in the system.webServer section:
<rewrite>
<rules>
<rule name="rewrite" patternSyntax="Wildcard">
<match url="/api/*" />
<action type="Rewrite" url="/{R:1}" />
</rule>
</rules>
</rewrite>
You will need to use the correct capture groups to match your situation.

Related

IIS URL Rewrite is not working when redirecting to another server

I need to redirect links to a different server using URL rewrite in IIS. These are SRSS report links. I have tried so many different configurations i have given up.
Old Link:
http://OldServer/ReportServer/Pages/ReportViewer.aspx?(reportnamehere - need to copy this part across to new link)
http://NewServer/ReportServer/Pages/ReportViewer.aspx?reportnamehere
Any ideas how this can be achieved?
This is a very simple rule requirement. Something like this will work. Basically just redirects to a new server appending the entire URL
<rule name="Redirect to newServer" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://NewServer/{R:1}" redirectType="Temporary" />
</rule>

Remove Subdomain from URL in IIS or ASP.net MVC or haproxy

Is it possible to remove a subdomain (or basically treat it like www) from the URL?
Example:
subdomain.domain.com/specific/link/forsubdomain -> domain.com/specific/link/forsubdomain
and not have it point to the primary domain and return a 404 error,
example:
domain.com/specific/link/forsubdomain -> return a 404 because it only exists in the subdomain.
If it's possible to do something in Haproxy as well Or disguising URLs in ASP.net MVC Route table modifications im open to it.
Not just IIS configuration.
Just wanted to know if it's possible to change the URL as i described and still have it point to the subdomain site.
If I understand your question correctly you are refering to canonical URLs.
Within IIS you can use URL Rewrite, which is a module you can install manually or via the Web Platform Installer (if it's not already installed).
This allows you to create a url rewrite rule which you can configure per site, something along the lines of;
<rule name=”Redirect URL to WWW version”
stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
</conditions>
<action type=”Redirect” url=”http://www.example.com/{R:0}”
redirectType=”Permanent” />
</rule>
You can add the rules manually to the site's web.config file or use a GUI within the IIS site manager tool.
There's a lot of reference and a number of tutorials available from Microsoft and numerous bloggers.
An example of a complete web.config just doing this type of redirect.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WWW Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^contoso.com$" />
</conditions>
<action type="Redirect" url="http://www.contoso.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Custom Path with ImageResizer SqlReader

I am developing an application that will have friendly urls to dealer logo images and I would like to use the SqlReader Plugin for ImageResizer.
In the examples I read on their website, you can easily host files from SQL all using the same paths (e.g. ~/databaseimages/{id}) but I'd really like to be able to do something to the effect of ~/michigan/dealers/{id}/logo.jpg where michigan could potentially be any state name.
Is it possible to have a multi-part id (state & id) and have the filename be static as I've shown, or is there a better way to do this?! I really would prefer not to have to change the url if I don't have to.
Solution
Based on the accepted answer, this is the URL Rewrite solution I used
<rewrite>
<rules>
<rule name="Rewrite to dealerlogos SqlReader" stopProcessing="true">
<match url="^([_0-9a-z-]+)/dealers/([_0-9a-z-]+)/logo.jpg" />
<action type="Rewrite" url="dealerlogos/{R:2}?regionSlug={R:1}" appendQueryString="true" redirectType="Found" />
</rule>
</rules>
</rewrite>
You can do this with URL rewriting of any kind.
ImageResizer provides a built-in event ImageResizer.Configuration.Config.Current.Pipeline.Rewrite that you can attach a handler to if you want to rewrite the path only for ImageResizer requests.

Force a language prefix in the url with Sitecore

Is there a setting to force the language in the URL? Like, if I browse to http://www.site.com, I should be redirected to http://www.site.com/en, as it is now I can see the start page without the language prefix.
The LinkManager is configured to always put in the prefix so all the links look fine at least.
Another way to use is our SEO Friendly URL Module.
This module implements a custom LinkProvider that provides SEO Friendly URL's and forces items to be accessed through their friendly URL.
So if an item is accessed without the language code in the URL (e.g. /my-item) the module will 301 redirect to the URL with language code (e.g. /en/my-item).
That is, if you have configured it to force friendly URL's (forceFriendlyUrl="true") and set languageEmbedding="always".
We use that module on our corporate website, so take a look at that to see it in action.
You can use ISS URL Rewrite to redirect / to /en
Install that module on the IIS, and setup a rule.
It can be done from the IIS gui or from web.config
I am using this rule to do the same.
Insert the following configuration in the system.webServer section in web.config
<system.webServer>
<rewrite>
<rule name="redirert / to en" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{PATH_INFO}" pattern="^/$" />
</conditions>
<action type="Redirect" url="/en/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
It will redirect every request hitten "/" to "/en"

IIS 7.5 URL Rewrite - Redirect from http to https for account controller but from https to http for everything else

I've found bits and pieces of what I need to make this work, but haven't been able to bring everything together into a workable solution.
I am working on an intranet site, and want to secure *just the logon and logoff actions on my account controller with https. I have the certificate installed correctly, and can successfully redirect traffic to these controller actions to https using a UrlRewrite rule:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="^account/logon$|^account/logoff$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
Now, however, I also want to redirect *all of the rest of my site's requests (other than traffic to the two actions) back to http. I'm not interested in debating the merits of this approach, as I have what I consider valid reasons for wanting to redirect back out of https to http.
I've tried writing some code in the Actions to achieve this, but am having major issues with this. I don't know if it is because I'm working with two load-balanced servers or what, but anything I try just gives me a "too many redirects" error message.
So, two questions:
Is it better to use a UrlRewrite rule to redirect out of https or a controller actions?
Does anyone have a working code example or something that can at least get me started down the right path?
Any help is much appreciated!
Better late than never. This might help you or someone else.
<rule name="Redirect to HTTP">
<match url="secureDir/(.*)" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="secureDir/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
First, you will have a bit of a challenge here insofar as non-page resources -- how are you referencing images and stylesheets?
As for the question at hand, I think you want to force this upstream of the web servers, such as on the load balancer. I would also backstop the account controller by adding a RequireHttps attribute.
Finally, remember that even if the login process is secured, if that cookie is not transmitted over HTTPS you can easily end up with a firesheep like scenario.
I have some code here which lets you control this with attributes.
MVC already has a [RequireHttps] attribute which you would apply to your logon/logoff pages. My code extends this approach and gives you an additional [ExitHttpsIfNotRequired] attribute. With this attribute applied to your base controller, when you try to access any action with HTTPS that doesn't have [RequireHttps], it will redirect you to HTTP.

Resources