ASP.NET MVC 4 public folder like in Ruby on Rails - asp.net-mvc

Is there a way to give an ASP.NET MVC 4 project a "public" folder, which would act like the application root, like in Ruby on Rails?
ASP.NET MVC usually comes with a "Content" folder where you can put statically served files like style sheets, scripts, and images. But the URLs for these files have to include the Content folder, for example <img src="/Content/logo.png">.Is it possible to make the Content folder the root of the application so you can use this instead: <img src="/logo.png">?

I'm not sure why I didn't think of this before, but I was able to accomplish this with IIS URL Rewrite. The following rules assume a folder named "Public" where your static files reside. Of course, you can name the folder whatever you want.
<!-- Any direct references to files in the Public folder should be
301 redirected to maintain canonical URLs. This is optional. -->
<rule name="Public folder canonical path" stopProcessing="true">
<match url="^public/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>
<!-- Any URL that points to an existing file after prepending "Public" to it
will serve that file. For example, if a file exists at /Public/style.css
then the URL /style.css will serve that file. Likewise, if a file exists
at /Public/images/logo.png, then the URL /images/logo.png will serve
that file. Files in the Public folder will take precedence over files in
the application root, so if a file /Public/script.js exists and a file
/script.js exists, only the /Public/script.js file will be served. This
also takes precedence over MVC routes. -->
<rule name="Public folder" stopProcessing="true">
<match url=".+" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{URL}" pattern="^/Public/" negate="true" />
<add input="{DOCUMENT_ROOT}/Public{URL}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="Public/{R:0}" appendQueryString="true" />
</rule>

Related

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>

How to remove/disable mywebsite.azurewebsites.net domain after adding custom domain?

I have created a website on Azure, and linked it with custom domain name(CNET).
Now, when I look at domains on website configuration on Azure panel I see both www.mywebsite.com , and default mywebsite.azurewebsites.net. Both of these domains work fine and I can access website using any of these.
How can I remove mywebsite.azurewebsites.net domain? Does having both of these domains affect SEO?
EDIT*
Thanks for answers, I am trying to enable a 301 redirect, but it is not working. I have added this to web.config file ("example" being my actual site name)
<system.webServer>
<rewrite>
<rules>
<rule name="SEOAzureRewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.azurewebsites.net$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
I run the website but nothing happens. I can still access the mysite.azurewebsites.net address.
there is no problem in having both domains pointing to your website.
for SEO, you must do 301 redirect from mywebsite.azurewebsites.net to www.mywebsite.com.
It has to be 301 redirect. As this will tell the search engines to always index www.mywebsite.com
Since I couldn't set up the url rewrite in web.config, I created global filter to check for azure url and display error if so. Here is the filter, and I added a new view in my error pages for this purpose that just says "Page doesn't exist" to avoid indexing by search engines. Think this will solve possible duplicate indexing issues.
public override void OnResultExecuting(ResultExecutingContext context)
{
if (context.RequestContext.HttpContext.Request.Url != null)
{
string path = context.RequestContext.HttpContext.Request.Url.AbsoluteUri;
if (path.ToLower().Contains("mywebsite.azurewebsites") && !path.ToLower().Contains("error/oldazuresubdomainredirect650"))
{
throw new HttpException(650, "Azure legacy");
}
}
}
You can create a url rewrite rule to do exactly that
You just need to add a redirect rule to your site’s web.config file. You can do that by adding the following rewrite rule to the web.config file in your wwwroot folder. If you don’t have a web.config file, then you can create one and just paste the text below into it, just change the host names to match your site’s host names:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^yoursite\.azurewebsites\.net$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The above is all you need to do to get it to work. If you'd like to better understand what exactly that code does and why it works, there's a detailed writeup at https://zainrizvi.io/blog/block-default-azure-websites-domain/

Using URL rewrite with sub domains; download file fails

I currently have this rewrite URL rule in my web.config file
<rule name="Rewrite to qa" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^qa.golfgameskeeper.com$" />
</conditions>
<action type="Rewrite" url="qa/{R:1}" />
</rule>
it works great, makes qa.golfgameskeeper.com/qa work like qa.golfgameskeeper.com
However, when I try to download a file from a sub-directory of qa it seems to get confused
http://qa.golfgameskeeper.com/apps/iOS will list the file, but will not allow me to download it.
Even when clicking one the link above the rule re-writes the link to
qa.golfgameskeeper.com/qa/apps/iOS (cut and paste will work, not clicking the link)
Is there a way to modify this rule to allow what I'm trying to do? So, as I'm writing this I figured out what I am trying to do.
have
qa.golfgameskeeper.com -> qa.golfgameskeeper.com/qa (works)
and allow
qa.golfgameskeeper.com/apps/iOS to download the file without rewriting the URL twice (which is what I think it does).
Thank you,
You can add a rule before your rewrite rule to match on the apps subdirectory and do nothing, basically skipping the rewrite rule.
<rule name="Skip apps" stopProcessing="true">
<match url="^apps/(.*)" />
<action type="None" />
</rule>
<rule>
//your rewrite rule here
</rule>

Route static files to new path in asp.net mvc

I have 1000 static html page files and html data in Database.
Now i want to move to amazon s3 server, i will move all of my static files to s3.
How i can route the path to the new location, i can't update all of the pages and correct images path.
like:
<img src='/myfiles/images/blog/20140606/flow.jpg'/>
to
Request: /myfiles/images/blog/20140606/flow.jpg
Redirect to: htps://xxx.s3.amazonaws.com/myfiles/images/blog/20140606/flow.jpg
i need a route to redirect the request to new location instead of update all files.
May be using a url rewrite in your web.config like this:
<rewrite>
<rules>
<rule name="Rewrite to s3" stopProcessing="true">
<match url="^/myfiles/images/blog/(.*)" />
<action type="Rewrite" url="s3/mybucketname/{R:1}" appendQueryString="false" redirectType="Found" />
</rule>
</rules>
</rewrite>

Rewrite images directory IIS

We are migrating a legacy application to ASP.NET MVC, but in the old application the images folder was in the root of the app, so to reference an images the path would be /images/imagename.png, but we want to move the images in to /Content/images in the new application but we dont want to change the paths in all the css and the html.
Is there a way to do a rewrite to the images folder so that every URL that goes to /images goes to /Content/images instead?
Use URL Rewrite. You need to install it via WebPI, then add the following to your web.config in system.webServer:
<rewrite>
<rule name="Old Images" stopProcessing="true">
<match url="^images/(.+)$" ignoreCase="true" />
<action type="Rewrite" url="content/images/{R:1}" appendQueryString="true" />
</rule>
</rewrite>

Resources