How to rewrite url name from webconfig - c#

i want to rewrite url name from WebConfig file..I have old url like this
http://www.exmaple.com/'blogdetails'/some name/10082
Now my url is being changed to
http://www.exmaple.com/blog/details/some name/10082.
blogdetails is being chnaged to blog/Details..
Actually i waana redirect the url page when url will find a keyword 'blogdetails'..It will redirect to http://exmaple.com/blog page..i want to do this from web config file..
example like if i enter the old url like http://www.exmaple.com/'blogdetails'/somename/10082
this link contain "blogdetails" it will redirect to http://www.exmaple.com/blog.
i already got some code from internet some thing like..cause new link is being changed to blog/details..i dont wanna redirect to new link i just wanna redirect to this link something like this:- http://www.exmaple.com/blog
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^blogdetails$" />
<action type="Rewrite" url="blog" />
</rule>
</rules>
</rewrite>
</system.webServer>
how to do this???can anyone help me please?????

If you are expecting blogdetails to always be at the root, I think this might work:
^blogdetails.*$

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" patternSyntax="Wildcard" stopProcessing="true">
<match url=“^en/blogdetails$" />
<action type="Rewrite" url=“blog” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Try this one.This should work.

Related

If user is coming from domain testx.com, rewrite url to mydomain.com/testx

I have an ASP.Net website, but several domain names are pointing to the same website.
so for example lets say i have following domains
www.myMainDomain.com
www.mySite1.com
www.mySite2.com
I have 1 actual website with all the pages and subpages i want to have.
www.myMainDomain.com
www.myMainDomain.com/site1
www.myMainDomain.com/site1/products
www.myMainDomain.com/site2
www.myMainDomain.com/site2/products
...
So i would like to have a rewrite rule in my web.config
so that if the user is coming from the www.myMainDomain.com, it is just the normal site as it is existing
but if they are coming from www.mySite1.com, the rewrite rule should kind of take them to www.myMainDomain.com/site1, and if they go to a subpage, like products, it is always rewritten so like this
www.mySite1.com -> www.myMainDomain.com/site1
www.mySite1.com/products -> www.myMainDomain.com/site1/products
I have something like that, but this is not working, and it is actually causing a 500 Error
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/site1/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
What could be a correct solution?
I think you could use the inverse of standard canonicalhostname redirect with some changes:
<rule name="CanonicalHostNameRule_Mod" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.mySite1\.com$" />
</conditions>
<action type="Redirect" url="http://www.myMainDomain.com/site1/{R:1}" />
</rule>
In this way you have to make a rule for each secondary domain.

Redirecting an asp.net MVC URL to a specific controller/action/parameter using httpRedirect in web.config

I have a website hosted in Azure, which will be accessed with the URL https://abc.cloudapp.net/controller1/action1/parameter1
My Customer wants the following.
When we try to access the url "https://abc.cloudapp.net", it should automatically redirect me to the actual url mentioned above for the website.
I want this to be achieved using the web.config. and I tried several combinations (Couple of those are listed below).
Combination 1:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^abc.cloudapp.net$" />
</conditions>
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Combination 2:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url="https://abc.cloudapp.net" />
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
None of these combinations have worked.
I had posted a workaround below for this problem.
However, I need some quick help in this for a permanent solution.
I still need help on this, can some one help please.
I had found a workaround for this.
When we use the url "https://abc.cloudapp.net", using the default route config we can set this to go to action1 in controller1.
Then in "action1" of "controller1" if we see that the "parameter1" is null (must be delared as a nullable type), we need to initialize the parameter1 to a known value and redirect to the same "action1" in "controller1"
public ActionResult Index(int? parameter1)
{
if(string.IsNullOrEmpty(parameter1)
return RedirectToAction("action1", "controller1", new {#parameter1 = "knownValue"});
else
{
//whatever action you wanted to do based on value in parameter1
}
}
However this whole method calls for hard coding, We are assigning a know value to the paramter1. If there is a change in this value, we need to change the code
Hence I do not recommend this. However for now I have used this workaround. I still need a proper solution around this problem.
MY R & D Continues... ... ... ... ...

Editing configSource file at runtime

I want to edit a web application's config at runtime, specifically the file that is referenced by a configSource attribute. Whenever I try to open the file using the WebConfigurationManager, I always get the web.config, not rewrite.config.
Once I get it open, I want to edit it (e.g. clear out existing entries, add new entries) and save it back out to rewrite.config.
Am I better off just reading it in as a file (e.g. System.IO)
web.config
<system.webServer>
<rewrite>
<rules configSource="rewrite.config" />
</rewrite>
rewrite.config
<rules>
<rule name="rewrite1" stopProcessing="true">
<match url="^contact-us.html$" />
<action type="Rewrite" url="/contact-us" />
</rule>
<rule name="rewrite2" stopProcessing="true">
<match url="^about-us.html$" />
<action type="Rewrite" url="/about-us" />
</rule>
</rules>

ReRouting Http to Https in MVC 3.0 Using Rules Not Working

I have to route all the requests on my site from http to https, I was succesfull in doing that by adding another class in Global.Asax.Cs
// working part for redirecting http into https
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
But before that I tried by inserting Rules into WebConfig
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
Which is not working, But I have seen many posts here which say that Rules is the best way to route request, I have also installed URL Rewrite on my server . Cany any one please suggest why it wasnt working ( i am getting 500 internal server error).
Does rerouting in Global.asax affect performance when there is a huge volume of requests.
Please advice
I use this rule and it works like a charm:
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
without any datailed info about the error i can only guess that your web.config is malformed or you put the rule section in the wrong place or URL Rewrite Module is not installed correctly

Change URL Rewrite Rule in Web.Config from Code C#

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.

Categories