Web.config mass 301 redirect old news page to a slightly different Urls - redirect

I need to redirect a bunch of news articles to a different url format using web.config.
For example, this Url:
https://www.example.com/footer/press-resources/press-releases/press-release-detail/abcd-press-release-item
Should be redirected to this Url:
https://www.example.com/about/press-releases/press-release-detail/abcd-press-release-item
In this example, as you can see this portion of the Url is staying intact /press-release-detail/abcd-press-release-item but everything else before this should be redirected. The only dynamic part of the Url is the article's title which is at the end of the Url, in this case: abcd-press-release-item
I have about 300 items, obviously redirecting them one by one is going to take some time. Is there a way to rule them all out and mass redirect the old Urls to the new ones?
This is a Windows/IIS server with a .NET CMS and a web.config file.
I tried this rule and it didn't work for me:
<rule name="301 Redirect 1" stopProcessing="true">
<match url="^https://www.example.com/footer/press-resources/press-releases/press-release-detail\$" />
<action type="Redirect" url="https://www.example.com/about/press-releases/press-release-detail" redirectType="Permanent" />
</rule>

Try to use this URL Rewrite rule:
<rewrite>
<rules>
<rule name="Test" stopProcessing="true">
<match url="(footer)/(press-resources)(.*)" />
<action type="Redirect" url="https://www.example.com/about{R:3}" />
</rule>
</rules>
</rewrite>

Related

URL rewrite loses Virtual Directory prefix using JavaScript root-based urls

We have some third-party service on http://localhost:1234/, and we want to be able to access it externally via https://my-site.com/MyService/
For that we have IIS with Virtual Directory (Application) MyService and URL rewrite rule
<rule name="my-service" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<action type="Rewrite" url="http://localhost:1234/{R:0}" />
</rule>
This service renders html with some root urls like Page 1
But following such links will lead us to https://my-site.com/MyService/page1.html
In order to fix it we have another URL rewrite outbound rule
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img, Link, Script" pattern="^/(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://my-site.com/MyService/{R:1}" />
</rule>
And it works fine and renders absolute urls Page 1
However, if we have JavaScript
// redirect
location.href = "/page2.html";
// AJAX
var xhr = new XMLHttpRequest();
xhr.open("GET", "/page2.html");
xhr.send();
// fetch
fetch("/page2.html");
those root-based urls will be resolved as https://my-site.com/MyService/page2.html
As this service is third-party, I can't just go to modify all those JavaScript code to include /MyService prefix. And those urls such as /page2.html are constructed dynamically so it's impossible to just use URL rewrite module to fixup corresponding JavaScript.
I was trying to add URL rewrite to the root website to capture such urls via http referer
<rule name="MyService" stopProcessing="true">
<match url="^(?!MyService).+$" />
<conditions>
<add input="{HTTP_REFERER}" pattern="https://my-site.com/MyService*" />
</conditions>
<action type="Redirect" url="https://my-site.com/MyService/{R:0}" redirectType="Found" />
</rule>
but it still doesn't work properly and behaves very fragile.
Is there a way to solve such supposedly very common task easily?

iis web.config redirect certain domain/path to a new domain/samepath

I need to use web.config to setup a redirect for a certain path/folder to a new domain using the same original path/folder.
Ex:
If www.domain1.com/path2match1/* is detected then redirect to:
www.newdomain.com/path2match1/* (same path as matched on domain1)
ex:
current: www.domain1.com/path2match1/someFileOrFolders/somefile.html
redirect to: www.newdomain.com/path2match1/someFileOrFolders/somefile.html
Any help would greatly be appreciated
thanks
I managed to find an answer for my question on my own! (Yay for me! haha)
Anyways,
Here's how to do what I was asking:
<rewrite>
<rules>
<rule name="NAME" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="(.*)/FOLDER/(.*)" />
</conditions>
<action type="Redirect" appendQueryString="false" url="http://DESTINATION-URL.TLD/FOLDERS/{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Caution! Adding multiple or tag set or placing this code in the wrong location will cause the site to generate internal server errors, making it unavailable for visitors until the additional tags are removed.
NAME can be anything, as long as it is unique
/FOLDER/ is the folder you are redirecting from
http://DESTINATION-URL.TLD/FOLDERS/ is where you are redirecting to.

Creating a IIS Url Rewrite/Redirect

Im struggling to get a url rewrite/redirect to work in IIS. I've installed the url rewrite module and all the rules fail to do anything. Here is the scenario, we want all web requests which generate a report to be pushed off to a secondary server so it doesn't harm the main box. The web requests that generate reports look something like this:
http://mywebaddress/api/Actionname=GenerateReport&param=123
So im wanting to do some type of regex check on finding any web requests that have "GenerateReport" in it and redirect it to something like:
http://mywebaddressofsecondserver/api/Actionname=GenerateReport&param=123
Any ideas on how the redirect/rewrite would go for this?
You need to check if REQUEST_URI contains Actionname=GenerateReport.
If so, you'll redirect it to other webserver url equivalent.
Translated to an IIS rewrite rule, it would look like this
<rule name="Delegate report generation" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_URI}" pattern="Actionname=GenerateReport" />
</conditions>
<action type="Redirect" url="http://mywebaddressofsecondserver/{R:1}" />
</rule>
Thanks, Justin Iurman,
Your answer solved my issue of getting methods
http://mywebaddress/api/Param/Action1
http://localserverwithport/api/Param/Action1
but for below Post methods it's still giving 404 not found
http://mywebaddress/api/Param/PostAction2
http://localserverwithport/api/Param/PostAction2
Post parameters are:
{"Param1":"James","Param2":"jani"}
My implementation is
'<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite Rule1" >
<match url="^(.*)" />
<action type="Redirect" url="http://localserverwithport/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>'

Redirect rule in web.config

I was looking into this Seemingly simple redirect in IIS using web.config file because I have a similar situation, but the approach mentioned there doesn't seem to be working for me.
I have a help section on a web site, which for now I want it to redirect to another place. The rest of the site should stay the same, which means that only the help section/content should make the user go to another site:
device.domain.com/help
device.domain.com/help/version
Should make the request go to
company.custhelp.com
But for example device.domain.com/api should still work. This is what I tried. If I test the pattern inside of IIS it says that it will work. What am I missing?
<system.webServer>
<rewrite>
<rules>
<rule name="Help Redirect" stopProcessing="true">
<match url="(.*)/help(.*)" ignoreCase="true" />
<action type="Redirect" url="http://company.custhelp.com/" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
...
</system.webServer>
The match url will try to match on the path after the domain, starting from after the slash. So it should be like so:
<match url="help(.*)" ignoreCase="true" />
The match url that you wrote would match on device.domain.com/temp/help, but not device.domain.com/help.

Canonical Redirect with classic ASP shows site folder

If pointed to http://domain.com it redirects to http://www.domain.com/thesite/index.asp which is the actual location. No matter the page, it always appends the actual folder path.
ive been using this script for canonical redirection, included in every page.
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www." &_
Request.ServerVariables("HTTP_HOST")&_
Request.ServerVariables("SCRIPT_NAME")
End if
I have several sites in a shared hosting, each in its own folder.
How can i prevent this?
Thanx for your help
Ok after further diggin' i finally bumped into a solution. Turns out IIS7 had url redirection rules enabled, so this can be accomplished through web.config, like this
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I overlooked it before cause it was listed as ASP.NET solution, not classic ASP.
But there it is, solved.