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/
Related
My url like this : https://myhospital.com/Hospitals-and-Clinics/Hospitals/Hospital-A/Our-Doctors
I try this script :
<location path="Our-Doctors">
<system.webServer>
<httpRedirect enabled="true" destination="https://www.appointment.myhospital.com/" httpResponseStatus="Permanent" />
</system.webServer>
</location>
in the web config
But it does not works. It does not redirect to https://www.appointment.myhospital.com/
Hospital-A is name of hospital
So it can like this :
https://myhospital.com/Hospitals-and-Clinics/Hospitals/Hospital-B/Our-Doctors
or
https://myhospital.com/Hospitals-and-Clinics/Hospitals/Hospital-C/Our-Doctors
It's dynamic
How can I solve this problem?
Is using a regular expression for this case?
Your config does not work because you are wrong path.
It must be
<location path="Hospitals-and-Clinics/Hospitals">
You can use URL Rewrite Module module in your case. like this:
<rule name="Demo" stopProcessing="true">
<match url="\/Our-Doctors$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?myhospital\.com$" />
</conditions>
<action type="Redirect" url="https://www.appointment.myhospital.com/" />
</rule>
ref doc: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
In IIS 7 I've created a URL Rewrite rule which checks for URLs which don't end with a /
<rule name="AddTrailingSlashRule1" enabled="true" stopProcessing="true">
<match url="(.*[^/])$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
But for some reason this causes a problem with the ScriptResource.axd as the / gets added to the end of the file (ScriptResource.axd/?d=234...), how do I resolve this issue?
Any help would be appreciated.
Matt
ScriptResource.axd isn't actually a file. So I think your only option is to modify the url regex to not match when the url includes ScriptResource.axd (and aren't there others; webresource.axd).
Let us know if you need help with the regex.
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 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>
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.