web.config: redirect 301 doesn't work properly - redirect

I want to do a 301 redirect for all the requests on one site (also images, documents, and other files) to the homepage of another site.
I tried putting the web.config on the root of the site, and I'd insert this code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
<httpRedirect enabled="true" destination="http://www.newsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
But with this code on the web.config the problem is that if I digit:
http://www.oldsite.com/file.html
The browser redirect me to: http://www.newsite.com/file.html
But I need that all the redirects are on the homepage.
How can I do it?

You need to set exactDestination="true" to make the destination absolute:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
<httpRedirect exactDestination="true" enabled="true" destination="http://www.newsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
See: https://www.iis.net/configreference/system.webserver/httpredirect

Related

how passing parameter ?id=x inside location path web.config

I'm trying to redirect from web.config using this structure:
<location path="name-page.asp?id=1">
<system.webServer>
<httpRedirect enabled="true" destination="new-page.asp" httpResponseStatus="Permanent" />
</system.webServer>
</location>
But I get an error in name-page.asp?Id=1 because you can't use the "?" parameter, at least i think. How can I get around?

Having trouble with web.config redirect; 500 internal server error

I've created a new website and some old links don't work so I want to redirect those links to my new homepage with anchor tags. I have tried so many code snippets but nothing is working. Can anyone see what I've done wrong (I'm not a programmer) thanks! -
I just get:
500 - Internal server error. There is a problem with the resource you
are looking for, and it cannot be displayed.
Here is my code:
<location path="page_contact.html">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.mywebsite.com/index.html#contact" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="portfolio.html">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.mywebsite.com/index.html#portfolio" httpResponseStatus="Permanent" />
</system.webServer>
</location>

ASP.NET httpRedirect exactDestination for all pages except root

I'm changing the primary domain for an IIS website, and I need to keep existing URLs working. At the same time I am changing the behaviour of the start page of this (the previous) domain. This domain has previously been accessible at its root level, but now I need the root page (default document) to redirect to a specific subdirectory.
In summary: I need to, while redirecting, keep the trailing path for all pages except for the default document (root). Example:
www.old.com/ -> www.new.com/en
www.old.com/en/page2.htm -> www.new.com/en/page2.htm
www.old.com/en/page3.htm -> www.new.com/en/page3.htm
www.old.com/page1.htm -> www.new.com/page1.htm
I have this web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com" httpResponseStatus="Permanent" exactDestination="false" />
</system.webServer>
<location path="Default.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.new.com/en" httpResponseStatus="Permanent" exactDestination="true" />
</system.webServer>
</location>
</configuration>
I have set the IIS setting Default Document to Default.aspx.
But this doesn't work.
I solved it by using the URL Rewrite Module in IIS instead:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root request" enabled="true" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://www.new.com/en" redirectType="Found" />
</rule>
<rule name="Path request" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<action type="Redirect" url="http://www.new.com/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

authentication for perl(awstats)

i am using awstats for my IIS7.5
I try to use forms authenrication, but it only work on the directory only( http://XXXX.com/awstats/wwwroot/cgi-bin/ ).
However, I can go to http://XXXX.com/awstats/wwwroot/cgi-bin/awstats.pl?config=testsite directly without login.
I want people to to execute this (awstats.pl?config=testsite) before login.
I don't want to create a local domain for the users becasue I will have many user to using awstats in different subfoler.
The web.config in the /awstats
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms name="SIPE_ASPXAUTH" loginUrl="~/Login.aspx" protection="All" path="/" timeout="20">
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="Perl" path="*.pl" verb="GET,HEAD,POST" modules="CgiModule" scriptProcessor="C:\Perl64\bin\perl.exe "%s" %s" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
</handlers>
<httpErrors errorMode="Detailed" />
</system.webServer>
</configuration>

Using web.config file for redirect

Using only the web.config file, I need to redirect from one domain to another, and I also need to redirect each individual page (there are 25 of them) in my website to its corresponding page under the new domain. And the renamed /index.htm page, which is the home page (same as the domain name alone) and which is /ghostwriter.htm needs to be redirected to its corresponding page, which will also be the new home page.
Thus: http://www.rainbowriting.com/ is the same page as http://www.rainbowriting.com/ghostwriter.htm (the index page).
You need to make sure ASP.NET is handling .htm files (set this in IIS). Then configure web.config:
<configuration>
<location path="oldPage1.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.newDomain.com/newPage1.htm" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="oldPage2.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.newDomain.com/newPage2.htm" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<!-- etc. -->
</configuration>