Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect from a 404 page to new page using ASP.Net

Apologies if this has been asked...couldn't find any good answers. There are some ASP tutorials that shows this code:

    <%
    Response.Redirect "http://www.w3schools.com"
    %> 

but where do I put that code at if the original file is non-existant? and don't I have to put the original file into the code to tell the server to go from OLD file to NEW file if people try to access the old file?

I know how to do a redirect for a server which can accept redirects using PHP in an .htaccess file. But this site I am working on won't accept the code I have which usually works.

The 404 page will show:

Server Error in '/pagehere' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /pagehere

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280

I want to do a redirect from oldpage.php to newpage.php. oldpage.php is no longer existing.

Whate file do I create or edit and what code would I use for the redirect? Thanks!

like image 327
thomas Avatar asked Nov 09 '22 01:11

thomas


1 Answers

If you can control your web.config, you can add in permanent redirects.

A decent quick reference is at https://www.stokia.com/support/misc/web-config-response-redirect.aspx

From that site, you can do individual redirects.

<configuration>
    <location path="bing.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://bing.com" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    <location path="google.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://google.com" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    <location path="yahoo.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://yahoo.com" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
</configuration>

Here you would place oldpage.html under the location tag.

<location path="oldpage.html">

Then you would place newpage.html uder the httpRedirect tag.

<httpRedirect enabled="true" destination="newpage.html" httpResponseStatus="Permanent" />

Combined like this.

<location path="oldpage.html">
    <system.webServer>
        <httpRedirect enabled="true" destination="newpage.html" httpResponseStatus="Permanent" />
    </system.webServer>
</location>
like image 182
Kirk Avatar answered Nov 14 '22 23:11

Kirk