【问题标题】:Redirect from a 404 page to new page using ASP.Net使用 ASP.Net 从 404 页面重定向到新页面
【发布时间】:2016-02-28 08:00:07
【问题描述】:

抱歉,如果有人问过这个问题...找不到任何好的答案。有一些显示此代码的 ASP 教程:

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

但是如果原始文件不存在,我该把代码放在哪里呢?如果人们尝试访问旧文件,我是否必须将原始文件放入代码中以告诉服务器从旧文件转到新文件?

我知道如何为可以在 .htaccess 文件中使用 PHP 接受重定向的服务器进行重定向。但是我正在处理的这个网站不会接受我通常可以使用的代码。

404页面会显示:

“/pagehere”应用程序中的服务器错误。 无法找到该资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的网址:/pagehere

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.34280

我想从 oldpage.php 重定向到 newpage.php。 oldpage.php 不再存在。

我要创建或编辑什么文件,我将使用什么代码进行重定向?谢谢!

【问题讨论】:

  • 您可以配置一个自定义错误页面,该页面使用请求的 url 并重定向到新位置。但是为什么原始文件首先不再可用?不是您的部署的一部分吗?
  • 检查IIS Url Rewriter。它完全满足您的需求。
  • 井文件可以被删除或移动到其他地方。这不是我的网站。我只是在做这方面的工作。但该文件的位置仍被索引,因此最佳 SEO 做法是将文件 301 重定向到其新位置或执行所需位置。问题不应该是“为什么原始文件不存在”?我只需要知道使用什么重定向代码以及将代码放入哪个文件。文件可以在任何网站上删除或移动。这就是存在 404 和 301 状态代码的原因。
  • 确保发回正确的标题。 Response.Redirect 将 302 发送回浏览器。我相信您想在以下任一方面进行控制:a)您的 web.config,b)您的 global.asax Application_Error 或 c)IIS。以下是一些示例:stackoverflow.com/questions/667053/…

标签: c# asp.net redirect


【解决方案1】:

如果您可以控制您的 web.config,则可以添加永久重定向。

一个不错的快速参考是https://www.stokia.com/support/misc/web-config-response-redirect.aspx

从该站点,您可以进行单独的重定向。

<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>

您可以在此处将 oldpage.html 放在 location 标记下。

<location path="oldpage.html">

然后您将 newpage.html 放在 httpRedirect 标记之下。

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

这样组合。

<location path="oldpage.html">
    <system.webServer>
        <httpRedirect enabled="true" destination="newpage.html" httpResponseStatus="Permanent" />
    </system.webServer>
</location>

【讨论】:

  • 但是你怎么知道原始 URL在哪里呢?如果用户单击 oldpage.html,您将如何编辑代码以使 oldpage 重定向到新页面? oldpage.html 到 newpage.html?您的代码显示目的地,但不显示始发地。服务器向我展示了 web.config 文件,所以我假设我可以控制它。我试图通过 .htaccess 进行重定向,但这不起作用(我认为 htaccess 不适用于 asp)。
  • 我已经用更多细节更新了答案。基本上,您将原始页面放在location 标记内,将目标放在httpRedirect 下。如果您使用的是 IIS,在大多数情况下 .htaccess 将不可用。
  • 当我尝试它时给我一个 500 错误。这就是 web.config 文件的样子。请注意,出于安全目的,我删除了一些数据。 pastebin.com/FWKLuM9j 试图找出将重定向代码放在哪里。我将&lt;location&gt; 标记放在&lt;system.webServer&gt; 上方,将&lt;httpRedirect&gt; 标记放在&lt;system.webServer&gt; 下方,但出现500 错误...站点停机进行维护。
  • 试试这个版本pastebin.com/3940kzjB。您可以将它们单独放置在configuration 部分中。在这种情况下,我将它们放在connectionStrings 下方。看看它是否有效。
猜你喜欢
  • 2018-02-07
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多