ASP.NET Non existent URL files on server, use Rewrite? - url-rewriting

How do you get filenotonserver.html to show up in a browser? and not 404 error. How can I do a URL rewrite to match .html to default2.aspx, or vice versa??
Do this make any sense? I am newbie on URL rewrite for sure...
k, been looking around:
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^page$" />
<action type="Redirect" url="/page.html" />
</rule>
</rules>
</rewrite>
In the "match url" trying to match .html and action type would be default.aspx?p=whatevetmatched.html
Any ideas please?

ok so I got it:
<rewrite>
<rules>
<rule name="Redirect">
<match url="(.*)\.html$" />
<action type="Rewrite" url="default2.aspx?p={R:1}" />
</rule>
</rules>
</rewrite>
And on the default2.aspx reads the request("p") which has the "matched" html name and I then look it up in the database to display the data for that matched html "page"

Related

IIS Rewriting url with parameter

I'm trying to setup rewrite for url from /place/search-the-places/item-detail/id=123 to /place/explore-our-places/item-detail/id=123
but it doesn't work, what I'm doing wrong?
<rewrite>
<rules>
<rule name="Collections" stopProcessing="true">
<match url="^place/search-the-places/item-detail/id=([0-9]+)" ignoreCase="true" />
<action type="Redirect" url="/place/explore-our-places/item-detail/?id={R:1}" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
Thanks!
If you just want to match numeric id as a parameter, then instead of
id=([0-9]+)/([_0-9a-z-]+)
use
id=([0-9]+)
because the slash between regexes means your route is like this
/place/search-the-places/item-detail/id=123/end-of-route
and this is not true, I suppose.
Also try using type="Rewrite" instead of "Redirect".

Routing all requests for different domains to different folders on the same root

I posted a question up yesterday basically I have a single plan with winhost and I want to be able to have multiple domains (that are all pointed at my root) go to different subfolders and in these sub-folders I host different MVC apps. A very helpful chap took me most of the way but (and I know its lame how difficult im finding this) but I really cannot work out how to do the final peice...
The IIS config...
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
works brilliantly. When I now go to http://www.producerpte.co.uk I can see my site in the subfolder but the url looks like its on the root. However I also want to try and host my girlfriends site so I have tried multiple combinations but I just cannot seem to add rules that will work for both sites...I know I need to add two more rules like this...
<configuration>
<system.webServer>
<rewrite>
<rules><!--first site-->
<rule name="Redirect if producerpte" stopProcessing="true">
<match url="^producerpte/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder producerpte">
<match url="^.*$" />
<action type="Rewrite" url="producerpte/{R:0}" />
</rule>
<!--second site-->
<rule name="Redirect if samyoga" stopProcessing="true">
<match url="^samyoga/(.*)$" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite to sub folder samyoga">
<match url="^.*$" />
<action type="Rewrite" url="samyoga/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Whatever it is about the last rule I just cant get it to route. If I use it then when I go to Samyoga.co.uk I end up at Samyoga.co.uk/producerpte ?????
I tried matching the wildcard in the second rule (for each site) it just doesnt work at all then.....

ASP.NET 4.0 Rewrite Rules in Global.aspx File Issues with rewrite

I have a rewrite rule I'm using in ASP.NET 4.0 on IIS7:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="home.aspx" />
</rule>
</rules>
</rewrite>
<defaultDocument>
<files>
<add value="home.aspx" />
</files>
</defaultDocument>
</system.webServer>
This rule takes: (http:/example.com/aboutus.aspx) and it removes the .aspx from the end of the URL. I'm running into problems with wordpress being installed on my subdomain (http:/www.example.com/blog) I get the following error due to my rewrite rule:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /blog/.aspx
Does anyone know how I can fix the URL rewrite rule so it safely get to (http://www.example.com/blog/) and not add the .aspx at the end?
Is there some sort of syntax that can search for the blog subdirectory to ignore the directory '/blog/'?
Greatly appreciated! Thank you! :)
If you want your rule to be applied to every urls but the /blog/* ones, then you can use the negate option:
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^blog/" ignoreCase="false" negate="true" />
<action type="Rewrite" url="home.aspx" />
</rule>
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rule_pattern_properties

ISAPI PHP REWRITE

A friend is having me figure out a way to modrewrite on his windows server. He is running IIS 6 and Isapi is about the only thing I can find. I am not familiar with it and have read some of the documentation, but can't quite wrap my head around it. He is wanting to rewrite these URLS to make them clean
www.domain.com/cat.php?CTGID=####
and
www.domain.com/pp.php?ID=##
How would I go about rewriting these two URLS to make them Clean in ISAPI. I have installed it on the Windows Server and do I put these rules in IISF.ini in his website folder or put these codes in the master IISF file? Any Help would be greatly appreciated!
Rewrite rules go in a web.config file or in the ApplicationHost.config.
As described in http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/
Examples
<rewrite>
<rules>
<rule name="Force WWW" 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>
<rule name="Redirect from blog">
<match url="^blog/([_0-9a-z-]+)/([0-9]+)" />
<action type="Redirect" url="article/{R:2}/{R:1}" redirectType="Found" />
</rule>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="article.aspx?id={R:1}&title={R:2}" />
</rule>
</rules>
</rewrite>
For IIS, if the ISAPI_Rewrite module is installed, $_SERVER["IIS_UrlRewriteModule"] is set and contains the version number of the module. So you can check for the existence of this server variable.

Redirect URL in IIS 7.5

I want to all www domain to naked via IIS Rewrite engine. There are many domains pointing to same application.
Here is my rule :
^(www.)(.*)$
Action Type : Redirect
Redirect URL : {R:2}
Redirect Type : Permanent
When i test pattern for www.xxx.com
R:0 => www.stackoverflow.com R:1=> www. R:2 => www.stackoverflow.com
and that is fine.
What is wrong? And also should I include "http://" ?
Something like ^(www\.)(.+)$ should work when matching the http_host, but it might be better to specify the domain. From what I know of IIS (not much) and what it says on the net, something like:
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Oh, you said for any domain. If you want to make sure it ends in .com it should be something like
^(www\.)(.+)(\.com)$ against HTTP_HOST
.. oh, if you do that you need to back reference, so try something like this:
<rewrite>
<rules>
<rule name="Redirect www.domain.com to domain.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The c:1 is a back reference

Resources