IIS 7 rewrite rule and Url.Content issues - asp.net-mvc

I have the following rule in IIS 7:
<rule name="Category" enabled="true">
<match url="^store/([0-9]+)/(.*)" />
<action type="Rewrite" url="store?cid={R:1}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
Which seems to work fine, however in my ASP.Net MVC 3 app I have several #Url.Content("~/") entries which are resolving to /store/ as the root as opposed to /. A typical url would be http://mysite.com/store/99/coats-with-hoods for example.
EDIT/UPDATE: I'm still pulling my hair out on this one, so I decided to look into Url.Content code-base and I noticed that it checks if the URL has been re-written (true) and if so it makes the path relevant, which in turn does not give me the absolute URL:
if (!PathHelpers._urlRewriterHelper.WasRequestRewritten(httpContext))
return contentPath;
string relativePath = PathHelpers.MakeRelative(httpContext.Request.Path, contentPath);
return PathHelpers.MakeAbsolute(httpContext.Request.RawUrl, relativePath);
Anyone know why this would be? I'm a bit confused as to why this is happening and how I can account for it in my application?

Ok once I realised that I was never going to be able to use IIS Rewrite against ASP.Net MVC I decided to use HttpContext.RewritePath instead, and now all appears to be working as it should.
This is quite a fundamental issue as it wasn't just Url.Content that was affected, it was controller routes too, I had a form on this particular page that was also incorrectly pointing to /store/ instead of /.

If your site is currently and will always be a the root of the domain/sub-domain (e.g. you always intend ~/ to mean site.com/) then lose the ~ and just make all the urls be /path/to/content. ~ does some wacky voodoo stuff -- as you've seen.

Scripts.Url really helps to keep your app root in place

Related

Redirect URLs with sitecore at the end

I would like to ask for your help about redirect with URLs which contains sitecore at the end.
What I want to do is that if my requested URL contains "sitecore" at the end I want redirect this to hostname.
e.g. www.mySiteName.com/sitecore redirect to www.mySiteName.com.
So I created simple pattern for this as first:
then I changed configuration above to only (.*)(sitecore)$.
But when I request URL what I get is:
www.mySiteName.com/sitecore/service/nolayout.aspx?item=%2fsitecore&layout=%7b00000000-0000-0000-0000-000000000000%7d&device=Default
instead www.mySiteName.com.
I dont understand why this behaves like this.
Appreciate your help
Thanks
I think you are over complicating your condition and potentially redirecting to the wrong reference.
Try
<conditions>
<add input={URL} pattern="(.*)sitecore$" />
</conditions>

IIS hosting ASP.NET MVC site gives http 404 for urls containing 'web.config'

Requesting a page from IIS (hosts ASP.NET MVC 3 site) with url containing web.config gives 404 error. Urls with app.config have no problem. Local Visual Studio development server has no issues with this type of urls.
1 - What are any other special words other than web.config, being handled this way by IIS?
In request filtering page/hidden segments tab this is the current state:
I guess these are not special words, because IIS handles words like bin, App_code etc without a problem.
Answer: I guess these are the words being handled by IIS this way. So these are the default words I think and this list is configurable (new items can be added to this list).
2 - Are there any quick fixes (like by web.config modification) to handle urls with these special words?
Btw, I am not trying to serve the web.config file. Url format is : www.mysite.com/es/web.config/1
This is part of the IIS configuration under the Request Filtering section:
You can add/remove filters.
However, I do believe this is a really bad idea to remove web.config from it.
http://www.iis.net/configreference/system.webserver/security/requestfiltering
Cause:
As you already shown a snapshot of IIS configuration. These are reserved folder & files in .Net application, so IIS want to preserve those for security.
URLs which contain these strings as returned as 404 response only if these comes in before ? AND exactly between 2 slashes /../ OR at last. Eg: www.example.com/bin/anything.ext OR www.example.com/folder/sub/web.config
IIS match these string anywhere coz, web.config can we at any directory level.
If anything is with those string are there THEN page will be served by IIS. Eg: www.example.com/bin-folder/anthing.ext OR www.example.com/sub/bin.html OR www.example.com/-web.confing/page.aspx are OK.
I recommend to use some other words with these strings OR use at end of URLs with extensions, so that it will not come between two slashes.
Eg: www.example.com/en-web.config/1 OR www.example.com/en/1/web.config.aspx
Even then I have one Tricky Solution:
If you really need these strings exactly without other words in URL THEN I recommend to use URL-ReWrite.. This may not be quick at whole but except 2nd step its quick and handy, coz second step depends on your application.
1- Add this rule in IIS at top level:
regexp match: (/web|^web)\.(config$|config/) //OR as your requirement
re-write to: handler.aspx?url={REQUEST_URI}
<rule name="web-config" stopProcessing="true">
<match url="(/web|^web)\.(config$|config/)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="handler.aspx?url={REQUEST_URI}" appendQueryString="false" />
</rule>
2- In handler.aspx (or in any other language page) check the url GET variable and respond accordingly.Request.QueryString("url")
Do it carefully coz here you are controlling security.
I suggest to include the actual page content to response in handler.aspx or handler.php only rather then redirecting etc.
Before including content verify URL first(by regular expression etc.), and include content hardcoded, do not take any part of URL in to variable and use that variable in response-inclusion-code.
3- After that at last from IIS manager, In a specific website go to request filtering->hidden segment tab and delete the desire string. Eg: web.config. This step can be done by web.config also:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" />
<hiddenSegments>
<remove segment="web.config" />
</hiddenSegments>
</requestFiltering>
</security>
Now, IIS will serve the page and your handler page will show the output with exact same URL in user browser.

How to have unique URLs for a single dynamic page?

I think my question may be worded incorrectly but heres what I want to do (for SEO purposes).
I have a page that gives a logo and description of a brand e.g. Volvo. I want to use this same page as a template for all manufacturers but just change a few words around to customize it for each manufacturer accordingly. So in the URL I pass it a variable of the manufacturer e.g. "www.example.com/cars.cfm?manufacturer=BMW" and it will show a page that gives information about BMW.
The problem is that for each manufacturer the information is still showing up as the same page "cars.cfm" in the address bar but really I want it to go to a URL like "www.example.com/manufacturers/volvo.cfm" so it appears as a unique page just for that brand. But at the same time I don't want to have to create a seperate CFM (or php/asp) page for each manufacturer.
Is there a clever way to do this at all? I imagine its something to do with URL rewriting but not sure. I am using IIS 7.5.
Creating numerous sub-folders for different car manufacturers is going to be very tedious. My site would also include other types of manufacturers for different products not just cars. I guess URL rewriting would be best but my idea was to have a different 'page' for each manufacturer so the SEO would be improved.
Basically....
www.example.com/manufacturers/audi.cfm
www.example.com/manufacturers/bmw.cfm
www.example.com/manufactueres/volkswagen.cfm
But really I want the data on each of those pages to come from a database which contains unique data for each manufacturer such the logo image and history description.
Here is an example of what I mean:
http://www.fivestarautocentre.co.uk <-- go to bottom of that page and you can see links to various manufacturers
Building on what everyone else said, start by creating your www.example.com/cars.cfm?manufacturer=BMW page and get that working.
Assuming you're on IIS you would then create a web.config file (if it isn't already there) in your root folder.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Cars-Rewrite">
<match url="manufacturer/(\w+)" />
<action type="Rewrite" url="/cars.cfm?manufacturer={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
My regex could be off, but maybe someone else could chime in with the correct regex.
You can certainly handle this with URL rewriting as others have suggested but I think I would take a different approach. Particularly for SEO purposes. The best solution for SEO will be an actual URL for each brand. With that in mind create your site accordingly. Don't use a generic www.example.com/cars.cfm page, use www.example.com/bmw, www.example.com/volvo, etc. You can still have a ColdFusion page under each of those folders that does nothing more than include your generic ColdFusion template from another location (or call a cfc). Since each folder will have it's own unique stub file you can pass the appropriate vehicle manufacturer when the generic ColdFusion template is called. For example, under the www.example.com/bmw page:
<cfset manufacturer = "bmw" />
<cfinclude template="/mytemplates/genericpage.cfm" />
Then your genericpage.cfm uses the assigned variable to display the appropriate text and graphics. You could even get around having to set a particular variable under each folder by parsing the URL and grabbing the manufacturer from it when the template is included and executed. I believe the cgi.script_name variable will contain the path needed to do this.

Umbraco rewriting URL based on old page

I just recently started working on a project that involves Umbraco. So, I am no expert on it (not even close). Please help.
The issue is, we had a page on our site that was example.com/careers.aspx. This page had the old content.
We have now redesigned it and moved the cotent "INTO" careers "folder". So, now it should work as example.com/careers/en/home.aspx.
However, when we go to that URL, it rewrites it as example.com/careers.aspx/en/home.aspx.
I looked for any rewrite config files but there are no rules (in the file; not sure about DB).
can someone please help? I am not sure what is happening here.
For additional information, when I look at "Properties" for /careers/en/home page then it shows "Link to Document" as "/careers/en/home.aspx".
Thanks
Presumably you are using Umbraco v4? In the web.config for a Umbraco v4 there is a umbracoReservedUrls appSetting key. You could add your path in here and Umbraco will essentially ignore rewriting it when it is requested.
The rewrite configuration is actually in config/UrlRewriting.config and this will allow you to control custom rewrites.
In the web.config you can created rewrite rules for the required pages.
In the < system.webServer> section create a < rewrite> < rules> section and then add for each page as required.
Would be something like this...
<rewrite>
<rules>
<rule name="RewriteCareers" stopProcessing="true">
<match url="careers.aspx" />
<action type="Rewrite" url="careers.aspx/en/home.aspx" />
</rule>
</rules>
</rewrite>

Redirect subdomain request to subdirectory in MVC

So, I currently have a simple MVC web site sat on www.mysite.com and an installation of BlogEngine in a BLOG folder on the root.
my question is how, in code, can I redirect a hit on sub.mysite.com to display the blogengine site in the BLOG folder?
The sites are currently hosted with WinHost who have suggested using a simple VBScript (seen here). This is fine but I can't for the life of me figure out where to put it within the MVC site. The link says the "default" document, but is that wihin the body/head? everything I have tried hasn't worked.
I know things are all working on the host side of things as hitting sub.mysite.com simply displays the MVC site as it should.
I have also read about the rewrite module but I feel this isn't an option because of limitations with my host.
Any help would be super awesome!
EDIT: Following Dan Atkinson's post below I have accessed the IIS7 Rewrite Module and created the following rule:
Input URL path after ‘/’ Matches (.*) Redirect http://www.mysite.com/blog/{R:1} (Stop processing = true, entry type = local)
{HTTP_HOST} Matches the Pattern ^blog.mysite.com$
{REQUEST_URI} Matches the Pattern /blog
the XML for this rull looks like this:
<rule name="blog.mysite.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^blog.mysite.com$" />
<add input="{REQUEST_URI}" negate="true" pattern="/blog" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/blog/{R:1}" />
</rule>
This still isn't working though and is still just defaulting to the root site..I feel we are close though! Thank you for all your help so far!
Do you have IIS7? If so, it may be better to do this as a redirect there instead. It will also perform better as well as the request won't even come into your MVC application.
Edit:
Someone else on WinHost posted this which may provide some help.

Resources