I have my site asp net pages at www.mydomain.com, and the blog was inside www.mydomain.com/blog. I had to move the blog to a subdomain, so I need to write a module to redirect any calls for any page within the blog to the new subdomain.
It works in my machine but not when I upload to the shared hosting. Any ideas what is wrong?
I wrote the following code
public void ProcessRequest(HttpContext context)
{
string sourceUrl = #"www.mydomain.com/blog";// #"localhost:51393/blog"
string destinationUrl = #"blog.mydomain.com/blog";
string currentLocation = context.Request.Url.AbsoluteUri;
if(currentLocation.ToLower().Contains(sourceUrl))
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 301;
System.Web.HttpContext.Current.Response.AddHeader("Location", currentLocation.Replace(sourceUrl, destinationUrl));
System.Web.HttpContext.Current.Response.End();
}
}
And added these handlers
<httpHandlers>
<add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
<add verb="*" path="*" type="MyHandler,App_Code.dll"/>
Any help is deeply appreciated.
I know it is pretty similar to this one httpHandler - subfolder issue but it didn't work.
Just use IIS Rewrite
Here's a similar code that redirects 1 URL to another. Little tweak will do it. I got this working with my smarterasp.net hosting account.
<configuration>
<system.webServer>
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
Related
I have the following two rules for my WCF:
<system.webServer>
<rewrite>
<rules>
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
The first rule:
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
Rewrites calls that get sent without the svc extension to the svc service.
The second rule:
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
Removes an extra path and directs to the correct service.
Everything works fine, however, the site I will be publishing to does not allow the <rule> tag to be used in the web.config. My question is, how can I modify my WCF to accomplish the two rules above programmatically. Basically duplicating the logic of the two items mentioned above in C# as part of the WCF service. Thank You.
I found the solution!
Very easy to fix, first remove the entire <rewrite> section from your web.config. Then just add new file (type global.asax) to your project if you do not already have it.
Add the following code in the Application_BeginRequest method:
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Raw URL
HttpContext context = HttpContext.Current;
string rawUrl = context.Request.RawUrl;
// Extra Path to be removed
const string EXTRA_PATH = "/MoreServices";
// Rewrite requests to /MyService to /MyService.svc
if (!rawUrl.Contains(".svc"))
{
// Index of last forward slash
int idxLastSlash = rawUrl.LastIndexOf('/');
// Insert .svc before last slash
rawUrl = rawUrl.Insert(idxLastSlash, ".svc");
}
// Remove extra path EXTRA_PATH from url
if (rawUrl.Contains(EXTRA_PATH))
{
// Remove extra path
rawUrl = rawUrl.Replace(EXTRA_PATH, "");
}
// Rewrite Path if it has been modified
if (rawUrl != context.Request.RawUrl)
{
// Note: Added "~" to avoid error: "The virtual path XX maps to another application, which is not allowed."
context.RewritePath("~" + rawUrl, false);
}
}
This will work the same way as the two rules above in the web.config. Hope this will help someone.
I have an ASP.NET MVC 4 web app. It has a default home page that handles all requests to http://www.mydomain.com as well as http://mydomain.com. I also have an MVC Area set up that can be accessed via www.mydomain.com/Foo/Hello/ (where "Foo" is the name of the area and "Hello" is a controller in that area).
If someone makes a request to foo.mydomain.com right now, it will route them to the default home page controller. I would like all requests to the foo subdomain root (eg. without a specific area/controller specified) to automatically be rerouted to the /foo/hello/ controller.
Additionally, I want any requests to the "Foo" Area via the www subdomain to be redirected to the "foo" subdomain. ie. I want all requests to www.mydomain.com/Foo/Goodbye to be automatically redirected to foo.mydomain.com/Foo/Goodbye
I have, of course, looked at lots and lots of other samples/questions, but none of them seem to address my exact issue.
I'm not sure if I should solve this issue with routes, route constraints, route handlers, etc.
Additionally: this application is hosted on Windows Azure Cloud Services, so I don't have full control over IIS settings.
In your Web Server, the Application Pool of your site must have Integrated PipeLine Mode as highlighted below..
or you can find it through the Basic settings of your Application Pool like below..
Then I added the HttpModule in my sample MVC application
HttpModule
public class MyModule1 : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
public void Dispose() { }
void context_BeginRequest(object sender, EventArgs e)
{
if (!System.Web.HttpContext
.Current
.Request
.Url
.Authority
.Contains("foo.mydomain.com"))
{
string URL =
(System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
"foo.mydomain.com" +
HttpContext.Current.Request.Url.AbsolutePath +
HttpContext.Current.Request.Url.Query);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 301;
System.Web.HttpContext.Current.Response.Status
= "301 Moved Permanently";
System.Web.HttpContext.Current.Response.RedirectLocation = URL;
System.Web.HttpContext.Current.Response.End();
}
}
}
Then i added some code n Web.config for my HttpModule
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<add name="MyModule1" type="rewrite.MyModule1" />
</modules>
<handlers>
<remove name="UrlRoutingHandler" />
</handlers>
</system.webServer>
<system.web>
<httpModules>
<add name="MyModule1" type="rewrite.MyModule1" />
</httpModules>
</system.web>
Then in the host file I added the following code.
127.0.0.1 foo.mydomain.com
Then I added the Bindings for my website
You can use URL Rewrite module for IIS7+ to achieve what you described. Documentation is here: URL Rewrite Module Configuration Reference.
Simplified example would be:
Rewrite URL so that foo.mydomain.com access /foo/hello (so no
redirect)
Redirect URL www.mydomain.com/foo to foo.mydomain.com/foo
Add in your web.config, system.webServer node rewrite section:
...
<rewrite>
<rules>
<rule name="Redirect mydomain.com/foo to foo.mydomain.com/foo" patternSyntax="Wildcard" stopProcessing="true">
<match url="foo" negate="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://foo.mydomain.com/{R:0}" redirectType="Temporary" />
</rule>
<rule name="Rewrite foo.mydomain.com to access /foo/hello" patternSyntax="Wildcard" stopProcessing="true">
<match url="" />
<conditions>
<add input="{HTTP_HOST}" pattern="foo.mydomain.com" />
</conditions>
<action type="Rewrite" url="/foo/hello" />
</rule>
</rules>
</rewrite>
</system.webServer>
You can also edit/view those rules in IIS Manager, using GUI. Click your web site and go to IIS section / URL Rewrite module. Bellow you can see 1st rule in GUI.
I have a website stored on azure for which I got an SSL certificate. I am trying to redirect www.mywebsite.com to https://www.mywebsite.com but with no success.
I am using the following code that should change the configuration of IIS where my project is deployed :
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
But when I type my URL it does not redirect to https.
By the way, rewrite appears unrecorgnized by Intellisense.
I can see that you've taken the code snipet from Steve Marx example
The problem is that you have whitespace in your rule name. Just remove them and it will work.
The name attribute of rule must not have spaces; the rule won’t work correctly in IIS on Azure with spaces in the name.
Find the full article here:
Azure https guide
I want to modify rewrite rule from C# code. Url Rewrite rule is resides in web.config file.
<system.webServer>
<rewrite>
<rules>
<rule name="partners">
<match url="^partners$" />
<action type="Rewrite"
url="partners.aspx" />
</rule>
<rule name="news">
<match url="^news$" />
<action type="Rewrite"
url="news.aspx" />
</rule>
<rule name="projects">
<match url="^projects$" />
<action type="Rewrite"
url="projects.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
I want to change for ex. <rule name="partners"> <match url="^partners$" /> to <rule name="partners"> <match url="^friendship/partners$" />,
how can I find node rule and update match url to "new one" where name = "partners";?
this is my idea for dynamic url rewriting. thanks for any other ways if you have.
I change value for connectionString in my web.config website with this code :
May be this example can help you (just change the value connectionString by system.webServer and add by rules etc..
Please tell me if it works for you
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load("../myPath/web.config");
foreach (XmlNode node in myXmlDocument["configuration"]["connectionStrings"])
{
if (node.Name == "add")
{
if (node.Attributes.GetNamedItem("name").Value == "SCI2ConnectionString")
{
node.Attributes.GetNamedItem("connectionString").Value = "new value";
}
}
}
step 1:- download urlrewrite2.exe Here
Step 2:- write you logic in web.config
<system.webServer>
<rewrite>
<providers>
<provider name="FileMap" type="FileMapProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
<settings>
<add key="FilePath" value="D:\j\branches\JuzBuzz\App_Data\rewriteurl.txt" />
<add key="IgnoreCase" value="1" />
<add key="Separator" value="," />
</settings>
</provider>
</providers>
<rules>
<rule name="FileMapProviderTest" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{FileMap:{R:1}}" pattern="(.+)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
step 3:- Put you .txt file in App_Code folder or another place for which you have given the address in web.config , txt file will have data like
**technology,expert/search-expert.aspx?CatId=1
counselling-personal-growth,expert/search-expert.aspx?CatId=2** etc**
Microsoft has Microsoft.Web.Administration.dll available to help you out, but it requires administrator permissions to execute,
https://www.iis.net/learn/manage/scripting/how-to-use-microsoftwebadministration
It is quite suitable for a WinForms application (such as IIS Manager) to control an IIS server, but can also be used in other types of applications.
I do have a personal project that is a custom MWA implementation that works for some non-administrator cases. If you are interested in it, let me know.
I am new to URLRewriting and trying to remove the .aspx extension using the following script in my web.config
<configuration>
<configSections>
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
</configSections>
<connectionStrings>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
However, I have no success with this. Moreover, the following code block is giving me an error.
<httpHandlers>
<add verb="*" path="*.aspx"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
> Could not load file or assembly 'URLRewriter' or one of its
> dependencies. The system cannot find the file specified.
Do I need to add the rewrite engine to my web application?
I have gone through this link but I could not get it.
Can any one suggest to me a step by step process or sample script please?
Use the following rule, works like a charm everytime!
<rule name="san aspx">
<!--Removes the .aspx extension for all pages.-->
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
Here is some nice rewritting engine with how-to's :)
http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET
http://www.codeguru.com/csharp/.net/net_framework/article.php/c19535/RewriteNET--A-URL-Rewriting-Engine-for-NET.htm
Seems like you have not added the dll Reference in the Web project for the class URLRewriter. Add this reference to fix this issue.
(As alternatives to writing your own)
IIS 7 URL rewrite module :
http://www.microsoft.com/en-gb/download/details.aspx?id=7435
IIS 5/6 :
http://iirf.codeplex.com/