How to host two applications under one domain - asp.net-mvc

At the moment I have a wordpress website that covers my blogs. I have a asp.net mvc application that is my ecommerce application.
At the moment I'm looking to have them both under one domain, I'm wondering is that possible?
When visiting
TestWebSite.com it'll point to my wordpress
Currently I asp.net mvc application set as a subdomain shop.TestWebsite.com but I would like to have it as TestWebsite.com/shop
What I'm currently having trouble with is configuring the asp.net mvc application such that when they visit www.TestWebsite.com/shop it'll load that application.
Any tips would be greatly appreciated

Yes, is it possible using the UrlRewrite Module. Add your custom rules in your root web.config. Also, in wordpress folder you will have to add the respective wordpress rewrites (.htaccess to web.config converter online)
<system.webServer>
<rewrite>
<rules>
<rule name="SUBDOMAIN" stopProcessing="false">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^SUBDOMAIN.domain.com$"/>
<add input="{PATH_INFO}" pattern="^/APPFOLDER/" negate="true"/>
<!-- This one! -->
</conditions>
<action type="Rewrite" url="APPFOLDER/{R:1}"/>
</rule>
<rule name="DOMAIN" stopProcessing="false">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^DOMAIN.com$"/>
<add input="{PATH_INFO}" pattern="^/WORDPRESSFOLDER/" negate="true"/>
<!-- This one! -->
</conditions>
<action type="Rewrite" url="WORDPRESSFOLDER/{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>

Related

IIS Rewrite and reverse proxy for multiple applications

I have an IIS 10 with a website configured on that. There are multiple Applications underneath that website.
MyWebsite/app1 - MyWebsite/app2 - MyWebsite/app3
I Have another server(let's call it EndServer) hosting 3 websites on 3 different ports.
well, now what I wanna do is using IIS as a reverse proxy to redirect and MASK the application 1 to one of those websites in 2nd server and application 2 to another one.
at the end, Users will enter https://mywebsite/app1 and they will see the contents of website 1 in the Endserver.
Note: it is important for me that end Users see the URL like as https://mywebsite/app1/
how shall I edit the Rule below:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://endserver:8052/{R:1}" />
<conditions trackAllCaptures="true">
</conditions>
</rule>
</rewrite>
Thanks
A.
According to your description, I suggest you could try to use below url rewrite rule.
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app1/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8052/{C:1}" />
</rule>
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{URL}" pattern="^/app2/(.*)" />
</conditions>
<action type="Rewrite" url="http://endserver:8053/{C:1}" />
</rule>
Thanks for your answer.
I got same error. as you see below /App1 is missing in the URL after localhost:Port
localhost:8888/assets/styles/Custom.css?m=1549903616.0
I believe the trouble is with rewriting the response URL. I don't know who can I add the missing part in URL.
Regards.

Custom Error Change defaultRedirect

I run a Sitecore multi site environment (c#/MVC). The same IIS website, same folder, same code is used for multiple websites. For example www.chalk.com and www.cheese.com, very different looking sites but both use the same IIS website (host headers), same folder, same web config, same database backend.
All is good except the error page. In the web.Config I have the usual setting:
<customErrors defaultRedirect="/error.html" mode="RemoteOnly" />
I need something like
<customErrors mode="RemoteOnly">
<error host="chalk.com" defaultRedirect="/chalkerror.html">
<error host="cheese.com" defaultRedirect="/cheeseerror.html">
</customErrors>
Is something like this possible?
In case anyone finds this question...
for me, I found the answer in Rewrite Rules.
NOTE: You need the URL Rewrite IIS Extention installed. (Download Here)
I added to the Web.Config
<system.webServer>
<rewrite>
<rules configSource="folder\filename.config" />
</rewrite>
</system.webServer>
And then a config file (filename.config)
<?xml version='1.0' encoding='utf-8'?>
<rules>
<rule name="Error-Chalk" patternSyntax="Wildcard" stopProcessing="true">
<match url="error.html" />
<action type="Rewrite" url="chalkerror.html" />
<conditions>
<add input="{HTTP_HOST}" pattern="*chalk*" />
</conditions>
</rule>
<rule name="Error-Cheese" patternSyntax="Wildcard" stopProcessing="true">
<match url="error.html" />
<action type="Rewrite" url="cheeseerror.html" />
<conditions>
<add input="{HTTP_HOST}" pattern="*cheese*" />
</conditions>
</rule>
<rules>
This line <add input="{HTTP_HOST}" pattern="*cheese*" /> makes the match on the domain name, so www.cheese.com or image.cheese.com will match. <match url="error.html" /> matches the page being asked for. <action type="Rewrite" url="cheeseerror.html" /> is the page IIS will serve. I can now have the different error page for the different site.
Apparently, this isn't achievable. customErrors settings work to redirect to error pages targeted for specific HTTP error codes.
However, As you mentioned, its an MVC application, you can create a Custom Error Handler MVC filter which checks for host\domain and based on that redirects to the required ErrorView.
Also, here are 6 great methods described to achieve expected behavior MVC Exception Handling

Globally modify left part URL in MVC 4 / 5

I am using IIS 8.5 with MVC4 and MVC5.
Let´s assume that I have two websites where the URL of the first (WS1) is:
http://myapp.example.com
and the second website (WS2) URL is:
http://myapp.example.com/sub
(and is located on another port to which I want to forward)
Now, my problem is that all URLs generated by MVC (links etc) will use the left part of the URL and remove /sub within that application. Let´s say for instance that in WS2 I want to redirect the user to http://myapp.example.com/sub/login but it will become http://myapp.example.com/login instead.
I have tried IIS URL rewrites but it will only work first-time request that actually contain the /sub part, the URLs generated by MVC of course remains erroneously without the /sub part.
<rewrite>
<rules>
<rule name="Rewrite">
<match url="^Sub/(.*)" />
<action type="Rewrite" url="http://myapp.example.com/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="Response Status Update" preCondition="ResponseStatus" stopProcessing="true">
<match serverVariable="RESPONSE_Location" pattern="^/(.*)" />
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<rule name="RewriteRelativePaths" preCondition="IsHTML">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<conditions>
<add input="{URL}" pattern="^/Sub/.*" />
</conditions>
<action type="Rewrite" value="/Sub/{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseStatus">
<add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
My question is how I can accomplish this. Using another subdomain is not an option.

Redirect to non-www-url domain with MVC4 / IIS7.5

First of all, I just spent a few hours on Stackoverflow reading similar questions but no answer seems to work for me.
I have an MVC4 application which has an SEO issue : the non-www is not redirected to www. I have installed the Rewrite module on the server (IIS7.5) and on my local environment (VS2010).
I tried many ways but none seems to work...
Ideally, I would prefer to have the rewrite rule in my MVC4 solution and avoid doing server side configuration as it is more convenient when deploying to new servers.
I tried to put the following code block in my web.config :
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<!-- code added for SEO non-www to www redirect -->
<rewrite>
<rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<!-- SEO code -->
</system.webServer>
I then published the solution (with the right domain name) but nothing changes. When trying to access http://domain.com, I just get a 404 not found error.
I tried several code block for this part but it just never work. I also tried to configure this on the server, which didn't work as well. The best solution would be to have this rewrite in my web.config so the rules are applied whenever I publish to a new web server.
Does anyone sees something wrong with this ? My web.config is in my MVC4 application and published via the Publish menu we get when right-clicking the solution in VS2010.
Thank you very much in advance for your replies.
I also tried this block for the rule but it doesn't change anything..
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />
</rule>

URL Re-writing using web.confg

This is what I'm trying to achieve using web.config (My current web.config is used to force non-www which will still be needed)
The site was first hosted under a sub-folder and we moved it now to the root. This is what I'm trying to achieve now:
old url example: mysite.com/subfolder/prodView.asp?idproduct=1312&idCategory=44 should get redirected to mysite.com/prodView.asp?idproduct=1312&idCategory=44 (We need to get rid of the old subfolder)
This is will be needed as many customer have already saved products link in their browsers and also we posted 1000's on links on twitter and facebook that all include the sub-folder within.
Any help will be highly appreciated.
Current web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW prefix" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" />
</conditions>
<action type="Redirect" url="http://mysite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Add the following rule to your existing rule(s):
<rule name="Redirect old subfolder links" stopProcessing="false">
<match url="^subfolder/(.+)" ignoreCase="true" />
<action type="Redirect" url="/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

Resources