如何仅当重写的目标 URL 存在时才使用 ISAPI-Rewrite 重写 URL?

发布于 2024-10-12 01:47:31 字数 871 浏览 2 评论 0原文

或者相反,如果 URL 不存在,我如何重写它?例如,拦截404。

上下文如下:我们正在慢慢地将我们的网站从 ASP classic 迁移到 ASP.NET。我所说的慢慢地,是指将 9000 多个 .asp 页面一次一页地转换为 .aspx。

在这样做的同时,我们希望防止任何损坏的链接,并避免丢失搜索引擎中的任何排名。这意味着我们所有的 .asp URL 必须继续存在,即使它们将由 .aspx 页面提供服务。

我希望能够做的是拥有 ISAPI-Rewrite 规则来重写 .asp -> .aspx 除非目标 URL 实际上不存在,在这种情况下它只会执行并返回 .asp。

因此,如果我们还没有将 somepage.asp 转换为 .aspx,则 URL 不会被重写,并且会显示 somepage.asp。一旦 somepage.asp 消失,URL 将被重写为 somepage.aspx,它将被执行并返回,但 Web 浏览器/搜索引擎会认为它收到了 somepage.asp。

将来的某个时候,我们会在以下位置设置规范网址: .aspx 页面,让搜索引擎在方便时赶上。

注意:我尝试过在 IIS 上使用自定义 404 来检查文件系统,并在目标 .aspx 页面存在时执行 Server.Transfer,但是您无法从 asp classic 传输到 asp.net。 (您得到“指定的'页面语言=“c#”'选项未知或无效。错误'8000d001'”)唯一有效的方法是我们进行301/302重定向而不是Server.Transfer,那就是此时对我们来说是不受欢迎的。 (管理层很偏执,说这不是一个选择。)

Or the opposite, how can I rewrite a URL only if it does not exist? eg, intercept 404s.

Here's the context: We are slowly migrating our site from ASP classic to ASP.NET. By slowly, I mean converting 9000+ .asp pages one page at a time to .aspx.

While doing this we want to prevent any broken links as well as avoid losing any rankings in the search engines. That means all our .asp URLs must continue to exist even though they would be served by .aspx pages.

What I would like to be able to do is have ISAPI-Rewrite rule(s) that will rewrite .asp -> .aspx unless the target URL does not actually exist in which case it would just execute and return the .asp.

So if we haven't converted somepage.asp to .aspx yet, the URL would not get rewritten and somepage.asp would be displayed. Once somepage.asp is gone the URL would be rewritten to somepage.aspx which would be executed and returned, but the web browser/search engine would think that it received somepage.asp.

At some point in the future we would set the canonical url in the .aspx pages and let the search engines catch up at their convenience.

Note: I have experimented with using a custom 404 on IIS to check the file system and do a Server.Transfer if the target .aspx page exists, however you cannot transfer from asp classic to asp.net. ( You get "The specified 'Page Language="c#" ' option is unknown or invalid. error '8000d001'" ) The only way this would work is if we did a 301/302 redirect instead of the Server.Transfer and that is undesirable for us at this time. (Management is paranoid and says not an option.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2024-10-19 01:47:31

就个人而言,如果内容正在移动,您可能需要编写一个执行 301 重定向的进程来指示页面已移动到新位置。事实确实如此。

这将确保新内容得到适当的索引和链接。

Personally if the content is moving you would want to write a process that does a 301 redirect to indicate that the page was moved to a new location. As that is truly the case.

This would ensure that the new content gets indexed and linked appropriately.

帥小哥 2024-10-19 01:47:31

我想通了。正如 Chris Haas 所建议的,如果我将 404 处理程序设为 .aspx 页面,那么当目标页面存在时我就可以执行 Server.Transfer,如果不存在,则直接失败并返回常规 404 状态。像这样的事情:

<%@ Page Language="c#" %>

<%@ Import Namespace="System.IO" %>
<script RunAt="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        String requestedUrl = Request.ServerVariables["HTTP_X_REWRITE_URL"];

        if (requestedUrl.EndsWith(".asp", StringComparison.CurrentCultureIgnoreCase)) {
            String targetUrl = requestedUrl + "x";
            String targetPath = Server.MapPath(targetUrl);

            if (File.Exists(targetPath)) {
                Server.Transfer(targetUrl);
            }
        }
        Response.Status = "404 Not Found";
    }

</script>

我仍然需要使用查询字符串来测试它,但到目前为止它可以完美地处理每种情况。当时间到来并且管理人员同意时,我们将停止执行 Server.Transfer 并将其更改为 301 Moved Permanently。由于 IIS 能够为每个目录设置错误页面,我们甚至可以在执行此操作时有所选择。

顺便说一句,看起来 ISAPI_Rewrite 3 可能有一个文件检查选项,也可以工作,但我们的服务器上只有 v2。

I figured it out. As Chris Haas suggested, if I make my 404 handler an .aspx page, then I am able to do a Server.Transfer when the target page exists and if not, just fall through and let the regular 404 status be returned. So something like this:

<%@ Page Language="c#" %>

<%@ Import Namespace="System.IO" %>
<script RunAt="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        String requestedUrl = Request.ServerVariables["HTTP_X_REWRITE_URL"];

        if (requestedUrl.EndsWith(".asp", StringComparison.CurrentCultureIgnoreCase)) {
            String targetUrl = requestedUrl + "x";
            String targetPath = Server.MapPath(targetUrl);

            if (File.Exists(targetPath)) {
                Server.Transfer(targetUrl);
            }
        }
        Response.Status = "404 Not Found";
    }

</script>

I still need to test it with querystrings but so far it handles each case perfectly. When the time comes and management is okay with it, we'll simply stop doing the Server.Transfer and change it to a 301 Moved Permanently. With IIS able to set error pages per directory, we can even be somewhat selective about when we do it.

Incidentally, it looks like ISAPI_Rewrite 3 might have a file checking option that could also have worked but we only have v2 on our server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文