5

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!

Croises
  • 18,570
  • 4
  • 30
  • 47
thomas
  • 163
  • 2
  • 13
  • You can configure a custom error page which uses the Requested url and redirect to a new location. But why wouldn't the original file no longer be available in the first place?? Isn't is part of your deployment ? – Dbuggy Nov 25 '15 at 13:46
  • 2
    Check the [IIS Url Rewriter](http://www.iis.net/downloads/microsoft/url-rewrite). It does exactly what you need. – Oguz Ozgul Nov 25 '15 at 13:57
  • Well files can be deleted or moved somewhere else. It's not my site. I'm just doing work on it. but the file's location is still indexed so best SEO practice would be to 301 redirect the file to its new location or do the desired location. The question should not be "why isn't the original file existing"? I just need to know what redirect code to use and what file to put the code in. Files can be deleted or moved on any website. That's why 404 and 301 status codes exist. – thomas Nov 25 '15 at 13:58
  • Make sure you send back proper headers. Response.Redirect will send a 302 back to the browser. I believe you want to control this in either: a) your web.config, b) your global.asax Application_Error or c) IIS. Here are some examples: http://stackoverflow.com/questions/667053/best-way-to-implement-a-404-in-asp-net – jdylanmc Nov 25 '15 at 19:01

1 Answers1

1

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>
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • but how do you know where the **original** URL is? if the user clicked on oldpage.html, how would you edit the code so that the oldpage redirected to the new page? oldpage.html to newpage.html? Your code shows the destination, but not the origination. The server shows me the web.config file so I assume I have control over it. I was attempting to do the redirection via .htaccess but that didn't work (I assume htaccess does not work well with asp). – thomas Nov 25 '15 at 17:10
  • I've updated the answer with some more details. Basically you place the original page within the `location` tag and the destination under `httpRedirect`. If you are using IIS, .htaccess wouldn't be available in most cases. – Kirk Nov 25 '15 at 17:56
  • Gives me a 500 error when I try it. This is what the web.config file looks like. Note that I deleted some data for security purposes. http://pastebin.com/FWKLuM9j Trying to figure out where to put that redirect code at. I put the ` ` tag above `` and the `` tag below `` but I get a 500 error...site down for maintenance. – thomas Nov 26 '15 at 02:57
  • Try this version http://pastebin.com/3940kzjB. You place these on their own, within the `configuration` section. I placed them below the `connectionStrings` in this case. See if it works. – Kirk Nov 26 '15 at 20:18