创建自定义301重定向页面 [英] Create Custom 301 Redirect Page

查看:147
本文介绍了创建自定义301重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用自定义路线写301重定向方案(类从RouteBase派生)类似<一个href=\"http://stackoverflow.com/questions/4970321/handling-legacy-urls-for-any-level-in-mvc#answer-4971478\">Handling传统的URL在MVC 的任何级别。我使用反射偷看入型Htt presponse类的永久重定向()方法,并注意到code两组的状态和输出一个简单的HTML页面。

I am attempting to write a 301 redirect scheme using a custom route (class that derives from RouteBase) similar to Handling legacy url's for any level in MVC. I was peeking into the HttpResponse class at the RedirectPermanent() method using reflector and noticed that the code both sets the status and outputs a simple HTML page.

    this.StatusCode = permanent ? 0x12d : 0x12e;
    this.RedirectLocation = url;
    if (UriUtil.IsSafeScheme(url))
    {
        url = HttpUtility.HtmlAttributeEncode(url);
    }
    else
    {
        url = HttpUtility.HtmlAttributeEncode(HttpUtility.UrlEncode(url));
    }
    this.Write("<html><head><title>Object moved</title></head><body>\r\n");
    this.Write("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n");
    this.Write("</body></html>\r\n");

这是可取的,因为在我的经验并非所有浏览器都配置遵循301重定向(尽管搜索引擎做的)。因此,它是有道理也给用户一个链接页面的情况下,浏览器不会自动去那里。

This is desirable, as in my experience not all browsers are configured to follow 301 redirects (although the search engines do). So it makes sense to also give the user a link to the page in case the browser doesn't go there automatically.

我想这样做是拿这个去一个新的水平 - 我要输出一个MVC视图的结果(连同其主题布局页),而不是在响应中有硬codeD丑陋通用的HTML。是这样的:

What I would like to do is take this to the next level - I want to output the result of an MVC view (along with its themed layout page) instead of having hard coded ugly generic HTML in the response. Something like:

    private void RedirectPermanent(string destinationUrl, HttpContextBase httpContext)
    {
        var response = httpContext.Response;

        response.Clear();
        response.StatusCode = 301;
        response.RedirectLocation = destinationUrl;

        // Output a Custom View Here
        response.Write(The View)

        response.End();
    }

我怎么能写一个视图的输出响应流?

How can I write the output of a view to the response stream?

其他信息

在过去,我们曾与来自mydomain.com 301重定向问题www.mydomain.com,并随后得到了很多来自用户的报告SSL证书无效。搜索引擎做他们的工作,但用户有问题,直到我切换到302重定向。实际上,我是无法重现,但我们得到的报告显著号码,这样的东西必须做。

In the past, we have had problems with 301 redirects from mydomain.com to www.mydomain.com, and subsequently got lots of reports from users that the SSL certificate was invalid. The search engines did their job, but the users had problems until I switched to a 302 redirect. I was actually unable to reproduce it, but we got a significant number of reports so something had to be done.

我打算使视图做一个重定向元以及JavaScript重定向,以帮助提高可靠性,但对于那些在301页谁仍然最终用户,我希望他们有宾至如归的感觉。我们已经有了自定义404和500页,为什么不能自定义主题301页呢?

I plan to make the view do a meta redirect as well as a javascript redirect to help improve reliability, but for those users who still end up at the 301 page I want them to feel at home. We already have custom 404 and 500 pages, why not a custom themed 301 page as well?

推荐答案

原来我只是过度思考问题和解决方案就简单多了,比我曾设想。我真正需要做的是使用的RouteData推请求到另一个控制器动作。是什么把我关是一个需要被附加到请求额外的301状态信息。

It turns out I was just over-thinking the problem and the solution was much simpler than I had envisioned. All I really needed to do was use the routeData to push the request to another controller action. What threw me off was the extra 301 status information that needed to be attached to the request.

    private RouteData RedirectPermanent(string destinationUrl, HttpContextBase httpContext)
    {
        var response = httpContext.Response;

        response.CacheControl = "no-cache";
        response.StatusCode = 301;
        response.RedirectLocation = destinationUrl;

        var routeData = new RouteData(this, new MvcRouteHandler());
        routeData.Values["controller"] = "Home";
        routeData.Values["action"] = "Redirect301";
        routeData.Values["url"] = destinationUrl;

        return routeData;
    }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        RouteData result = null;

        // Do stuff here...

        if (this.IsDefaultUICulture(cultureName))
        {
            var urlWithoutCulture = url.ToString().ToLowerInvariant().Replace("/" + cultureName.ToLowerInvariant(), "");

            return this.RedirectPermanent(urlWithoutCulture, httpContext);
        }

        // Get the page info here...

        if (page != null)
        {
            result = new RouteData(this, new MvcRouteHandler());

            result.Values["controller"] = page.ContentType.ToString();
            result.Values["action"] = "Index";
            result.Values["id"] = page.ContentId;
        }

        return result;

    }

我只是需要退回的RouteData,以便它可以由路由器来处理!

I simply needed to return the RouteData so it could be processed by the router!

请注意我添加了一个CacheControl头prevent IE9和Firefox从缓存中无法清除的方式重定向。

Note also I added a CacheControl header to prevent IE9 and Firefox from caching the redirect in a way that cannot be cleared.

现在我有一个显示的链接和消息给用户时,浏览器无法按照301一个漂亮的网页,我会加元重定向和JavaScript重定向到视图启动 - 赔率是浏览器将跟随其中之一。

Now I have a nice page that displays a link and message to the user when the browser is unable to follow the 301, and I will add a meta redirect and javascript redirect to the view to boot - odds are the browser will follow one of them.

这篇关于创建自定义301重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆