How to get IIS Express to serve static content above the root - iis-express

My home page, which is in my web root, has a relative url that starts with ../../lib/, so ../.. is above the web root.
When I try to serve my site with IIS Express, I get a 404.
How can I get this to work without moving the resources or changing the urls in the html page?

There might be a more direct way of doing this, but this is possible by adding the following to the web.config.
I noticed the 404 url was as if it was at the root, so the match url is at the root. I guess it just ignored the ../.. when interpreting the route.
First I set up a virtual directory on my local IIS to serve up the files above the root. I called it lib and pointed it at the content above the root. The actual path shown here is what I needed and just serves as an example.
<system.webServer>
<rewrite>
<rules>
<rule name="Files Above The Root" stopProcessing="true">
<match url="lib/(.*)" />
<action type="Redirect" url="http://localhost/lib/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
I'm using IIS Express just for debugging and it bears no relationship to how the site works in production. Coloring outside the lines is OK when it's just for debugging. The site build doesn't depend on this at all, and the config doesn't interfere with the site running in an actual environment because no urls are ever going to match this path.

Related

Redirect Rule For IIS Sharepoint Site

We have our Sharepoint 2019 site set up, and everything is running fine. However, when a user clicks the Brand Bar "Sharepoint" in the top left header, it brings them to a broken page. Researched this, and it seems like this link cannot be changed in 2019, as others are having this issue.
https://social.technet.microsoft.com/Forums/en-US/1ef910ec-fb70-443e-bcf4-2d277dc11d2a/sharepoint-2019-on-premise-management-shell-not-working-as-expected?forum=SP2019
What we wanted to do is a redirect, so whenever they went to the broken link, it would just direct them to the real homepage for our sharepoint.
Example -
Broken link is http://servername/my/_layouts/15/sharepoint.aspx
Correct link is https://sharepoint.companyname.com/SitePages/Home.aspx
Here is what I came up with to add to the web.config file, but doesnt seem to be working.
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*_layouts/15/sharepoint.aspx" destination="https://sharepoint.companyname.com/SitePages/Home.aspx" />
</httpRedirect>
</system.webServer>
</configuration>
Any idea on what needs to be done here? Is this even possible? I didn't set up the sharepoint site here so I'm not sure how http://servername/my/_layouts/15/sharepoint.aspx even became the brand bar link.
Thank you!
If you want to redirect from servername/my/_layouts/15/sharepoint.aspx to sharepoint.companyname.com/SitePages/Home.aspx by iis url rewrite, you can try this rule.
<rule name="test6" stopProcessing="true">
<match url="^my/_layouts/15/sharepoint$" />
<action type="Redirect" url="http://sharepoint.companyname.com/SitePages/Home.aspx" />
</rule>
This has been resolved.
Not sure what happened, only thing I did was place the rule on the top most part of the tree in IIS where the server name is. Before I was putting the rule on the actual site in the drop down. I guess this makes sense since the URL I wanted to redirect had the server name in it.

Subsequent IIS rewrite rules in web.config in (virtual) folder not being picked up

TL/DR: Can multiple rewrites (and a final redirect) for the same URI resource be chained over multiple web.config files living in subfolders along the URI's path during the same request?
I'm trying to migrate my classic ASP website from one shared virtual host to another; the new one using IIS7.5. My website has lots of subdomains, and the new hoster defaults to use one true website instance per subdomain, but my plan then only allows for a few of these subdomains, and since they're extremely low-traffic, one webserver instance should do perfectly fine. I therefore want the one main website instance just serving up all subdomains.
My path on their server is something like D:\websites\myaccount\mysite\wwwroot. For cleanliness sake (to separate all files for the subdomains in their own folders) I've set up a single folder for each subdomain in my hosting root folder (so at e.g. D:\websites\myaccount\subdomain1), and added virtual folders mapping names under the wwwroot to these subdomain folders (e.g. the virtual folder subdomain1 maps to D:\websites\myaccount\subdomain1).
I then added rewrite rules to the main wwwroot folder's web.config to intercept requests for the subdomains and rewrite them to map to the virtual folders;
<rule name="Subdomain support" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(subdomain1|subdomain2|subdomain3)\.mysite\.com$" />
</conditions>
<action type="Rewrite" url="{C:1}/{REQUEST_URI}" />
</rule>
Using this, requests to http://subdomain1.mysite.com/dir1/file.asp indeed do get served nicely via the subdomain1 virtual folder from D:\websites\myaccount\subdomain1\dir1\file.asp.
I now want each subdomain to be able to add it's own rewrite rules to the mix. The most important one is that requests for the root of the subdomain should get mapped to a default file in the most appropriate language subfolder. Directly inside the subdomain1 folder I thus have another web.config file, holding:
<clear />
<rule name="subdomain root redirect to proper language subpath" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_ACCEPT_LANGUAGE}" pattern="^(en|nl|de|es)" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{C:1}/index.asp" redirectType="Found" />
</rule>
<rule name="subdomain root redirect to default language subpath" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://{HTTP_HOST}/en/index.asp" redirectType="Found" />
</rule>
This should redirect the browser from subdomain1.mysite.com to subdomain1.mysite.com/en/index.asp (if your preferred browser language is set to "en.*").
However, I'm unable to get these rules to be picked up at all, no matter what I do. I've tested this by even matching .* and changing the redirects to point to e.g. google.com, but the server just serves me a 403 forbidden, and when I add:
<directoryBrowse enabled="true" />
I'm getting served the folder listing of the subdomain's folder itself. My redirects are starting to scream "Do I mean nothing to you?!", and I want to stop the screaming.
However, when I type in subdomain1.mysite.com/subdomain1/, the redirects do get picked up... I've also tried rewriting into regular (non-virtual) folders under the wwwroot and that exhibits the exact same symptoms.
Am I misguided in thinking this is possible? Could it be that since the original URI doesn't contain the (written-in) intermittant ...\subdomain1\... part in the folder path that IIS doesn't take that folder's web.config into account?
My first attempt was to use .htaccess (.irwaccess) files, but I soon found out they do not inherit (right?), so that was my reason to look into web.config files.
Your requirement is redirect subdomain1.mysite.com to subdomain1.mysite.com/en/index.asp.
Then rewrite to subdomain1.mysite.com/subdomain1/en/index.asp right?
If you enable FRT, you will see that redirect rule under subdomain1 will never been executed. This isby design.
In this case, you should just move all these rules to the root folder's web.config and modify the order like this:
Mix rule in root level and application level is supported by IIS but I think your rules can't be separated in this case.

Odd 404 during URL rewrite

Background: I'm forwarding incoming 80/443 traffic to \\SERVER2; TFS is running on \\SERVER3. I wish to route all TFS-related requests to \\SERVER3. I have to do it this way as I'm running Server Essentials on \\SERVER2, which is finicky enough to not work well under URL rewriting (almost as bad as SharePoint, but not quite).
Here's the only rule on the default website:
<rule name="TFS Rewrite" stopProcessing="true">
<match url="^tfs(.*)" />
<action type="Rewrite" url="http://server3:8080/{R:0}" />
</rule>
...and here's the Failed Request Log: https://1drv.ms/f/s!AodXF_j3BiWkhPAZwjnwC-rAecVgtw
Note the requested URL on line #87 of the PDF: http://server3:8080/tfs. I can browse to that internally just fine. The external URL is https://tfs.domain.com/tfs.
The next entry that's at all file-specific is the 404 itself, on line #165.
I just don't get this. It's a simple rule. Why would IIS turn up a 404 for a clearly valid and working URL?
EDIT
As a test, I added this condition:
<conditions>
<add input="{HTTP_HOST}" pattern="tfs.domain.com" />
</conditions>
Now if I browse to https://tfs.domain.com/, the default website loads.
This—together with the logs—would seem to indicate that while IIS is rewriting the URL, traffic isn't actually being routed to \\SERVER3.
What's going on here? This is a mystery.
OK, I got it working.
I'd installed both the URL Rewrite and ARR Modules, but I hadn't yet enabled proxy processing.
I created a dummy Reverse Proxy rule and was prompted to turn it on in IIS. I did so, deleted the dummy rule and now all is working as expected.

Duplicate domain in URL or Domain/AppName

I'm sure this is a simple issue, but I have noticed that when I host (shared host on GoDaddy) a website the URL repeats the Application name.
Example, I have http://makedifferences.org, if I go to this its fine, but if I click a link it takes me to http://makedifferences.org/makedifferences/...
It's not just this one site, it's all my sites on GoDaddy and I think it may be the same on my sites hosted elsewhere, but I can't check them rite now.
I'm not using web deploy to deploy it, as my first thought was it had to do IIS. My guess is that it must be a setting in Publish Settings, but I played around with them and couldn't seem to get it to go away.
Any advice would be appreciated.
Thanks,
Garrett
Update
This is apparently not a mvc thing as I check on a site I have hosted at dotnet-host.com and it doesn't have this problem. So I guess it is something with my settings at Godaddy.
I have deleted the folder and setup a virtual folder and this did not fix the problem.
I have multiple site hosting through them and my domain name A record is my dedicated IP for all the sites, then in host's domain management I point the domain name to the folder the site is in.
I think this is the correct way to do this but am not sure.
To restate my problem now that I know a bit more about it, if I type in http://makedifferences.org/Charities/Details/3 the page loads and everything is perfect. But if I click a link on the home page to go there the url is http://makedifferences.org/makedifferences/Charities/Details/3
Thanks why I thought it was a setting in Visual Studio.
There are two things that cause this behavior.
Your Application is installed in a folder instead of root directory for the shared hosting.
Your DNS settings point the domain name to this folder.
I suppose you are hosting multi-websites in shared hosting.
To get rid of this problem, you need to go in the IIS settings and set these folders as virtual directories.....Or create one for each website....And then install your application in that folder.
UPDATE
This is actually not an issue related to GoDaddy shared hosting at all but an issue with hosting an ASP.NET MVC site in a virtual directory. When you use the shared hosting provided by GoDaddy you get a root folder and limitless subfolders, each of which can be its own domain, by way of virtual directory. Unfortunately, MVC’s routing engine produces URLs that will include the virtual directory name appended to the domain name.
For example, let’s say you have a domain named http://www.example.com and your folder/virtual directory name is /File. If you take the MVC template project without making any modifications and upload it to your folder and then go to your url everything will look fine. You will notice the ‘Home’ and ‘About’ tabs at the top right of the page. When you click on the ‘About’ tab, since it is routed to the Home controller’s About action, you would rightly expect the URL to be www.example.com/Home/About. What you will see, though, is that the URL generated by the ActionLink method includes the name of the virtual directory. Therefore, the URL will be www.example.com/File/Home/About.
To get rid of this problem, add the following code to your Web.config’s system.webServer element
<rewrite>
<rules>
<rule name="Remove Virtual Directory">
<match url=".*" />
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>
</rewrite>
Solution seeked from HERE
Best of luck :-)
To work in all cases I used rewrite rule:
<rewrite>
<rules>
<rule name="Remove Virtual Directory">
<match url=".*" />
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>
</rewrite>
And the function bellow:
protected void Application_BeginRequest()
{
#region Godaddy shared host fix - Detect VDIR in url and remove
//verified that HTTP_X_ORIGINAL_URL keeps the original url (withoud domain) before url rewrite module,
//that way can check if the virtual directory name is at start, and remove it.
if (Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL"))
{
var origUrl = Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
var matchVdir = "/" + Myproj.Core.Constants.Environment.HostingVirtualDirectoryName + "/";
if (origUrl.StartsWith(matchVdir))
{
var urlFix = Request.Url.GetLeftPart(UriPartial.Authority) + "/" + origUrl.Remove(0, matchVdir.Length);
Response.RedirectPermanent(urlFix);
}
}
#endregion
}

intercepting url rewrite module, prevent rewrite

Is there anything that would cause the url rewrite module in IIS to not fire off? Maybe a site that is in integrated mode or an http handler?
I have tried a few different things to get the rewrite rules to work but nothing. My latest is as such
<rewrite>
<rewriteMaps>
</rewriteMaps>
<rules>
<rule name="rewriterule" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Redirect" url="http://www.google.com" />
</rule>
</rules>
</rewrite>
and it doesn't work at all. I've tried various regex, etc. Its like it doesnt get used.
Seems like you have your answer but here are some other possible things that might cause Rewrite Module not to work :
When you deploy your web site and see this feature not working on
your server, it is highly possible that you misconfigured something
on your server. One of the misconfiguration you might have done could
be setting the overrideModeDefault attribute to Deny for rules under
<sectionGroup name="rewrite"> inside your applicationHost.config
file.
If you are on a shared hosting environment and you see this feature
not working, then ask your provider if they have given you the
permission of configuring this part.
In your development environment, if you run your web site under
Visual Studio Development Sever, you won’t be able to see this
feature working. You need to configure your application to run under
at least IIS Express to see this feature working.
The answer in my case is that you have to be running the site in the same target platform as the url rewrite module. for example, I have x64 version of url rewrite module installed but the site was running under 32bit. Once I setup the site to run under 64bit, the rewrite started working.

Resources