How do I make IIS respond with 301 instead of 404?

Hi Experts.
Is there a way I can make IIS respond with 301 instead of 404?

I have this domain name that used to be a very powerful website. No matter what link is clicked I need to 301 it to somewhere else.

I set up a default.aspx file that has this code in it:

protected void Page_Load(object sender, EventArgs e)
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", "");

Open in new window

but that only catches it for a main domain name. So if someone goes to it works, but if someone goes to it doesn't and still 404s.
I need to make Google think there isn't any 404s. How can I do that ?

Please help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul MacDonaldDirector, Information SystemsCommented:
One way would be to create a custom error handler and parse 404 errors for requests to the old domain.  If you find a 404 for the old domain, you could direct the request to a page that has the 301 redirect built in.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vituxaAuthor Commented:
I found out another way to do this: I've added this in system.webServer tag in Web.Config:

          <rule name="Anything" stopProcessing="true">
            <match url="(.*)$" ignoreCase="true" />
            <action type="Redirect" url="" redirectType="Permanent" />

Open in new window

vituxaAuthor Commented:
but you did help me - Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft IIS Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.