简体   繁体   English

重定向URL

[英]Redirecting URL's

How can I redirect www.mysite.com/picture/12345 to www.mysite.com/picture/some-picture-title/12345? 如何将www.mysite.com/picture/12345重定向到www.mysite.com/picture/some-picture-title/12345? Right now, "/picture/12345" is rewritten on picture.aspx?picid=12345 and same for second form of url (picture/picture-title/12323 to picture.aspx?picid12323) I can't just rewrite first form of url to second because i have to fetch picture title from database. 现在,“ / picture / 12345”被重写在picture.aspx?picid = 12345上,并且对于第二种形式的url相同(picture / picture-title / 12323到picture.aspx?picid12323),我不能只重写第一种形式的网址第二,因为我必须从数据库中获取图片标题。

On the first hand, problem looks very easy but having in mind time to parse every request, what would be the right thing to do with it? 一方面,问题看起来非常简单,但由于要花时间解析每个请求,因此正确的做法是什么?

Not knowing what is the ASP.NET technology (Webforms or MVC) I will assume it's WebForms. 我不知道什么是ASP.NET技术(Webforms或MVC),我认为它是WebForms。

You can have a look at URL Redirecting and build you own rules. 您可以查看URL重定向并建立自己的规则。 And you do this one time only to apply to all of the links that look like you want. 而且,您只需要执行一次此操作即可将其应用于所有您想要的链接。

Scott Guthrie has a very nice post about it. Scott Guthrie对此有很好的帖子

If what you want is to when it comes to that address redirect to a new one, it's quite easy as well. 如果您想要的是将该地址重定向到新地址,这也很容易。

First of all let's reuse the code , so you will redirect first to a commum page called, for example, redirectme.aspx 首先,让我们重用代码 ,因此您将首先重定向到一个名为commitum的页面,例如redirectme.aspx

in that page you get the REFERER address using the ServerVariables or passing the Url in a QueryString, it's your chooise and then you can attach the title name, like: 在该页面中,您可以使用ServerVariables或在QueryString中传递Url来获得REFERER地址,这是您的选择,然后可以附加标题名称,例如:

private void Redirect()
{
    // get url: www.mysite.com/picture/12345
    string refererUrl = Request.ServerVariables["HTTP_REFERER"];    // using the ServerVariables or Request.UrlReferrer.AbsolutePath;
    //string refererUrl = Request.QueryString["url"];                 // if you are redirecting as Response.Redirect("redirectme.aspx?" + Request.Url.Query);

    // split the URL by '/'
    string[] url = refererUrl.Split('/');

    // get the postID
    string topicID = url[url.Length-1]; 

    // get the title from the post
    string postTitle = GetPostTitle(topicID);

    // redirect to: www.mysite.com/picture/some-picture-title/12345
    Response.Redirect(
        String.Format("{0}/{1}/{2}",
            refererUrl.Substring(0, refererUrl.Length - topicID.Length),
            postTitle,
            topicID));
}

to save time on the server do this on the first Page event 为了节省服务器时间,请在第一个Page事件上执行此操作

protected void Page_PreInit(object sender, EventArgs e)
{
    Redirect();
}

If you're running IIS7 then (for both webforms and MVC) the URL rewriting module ( http://learn.iis.net/page.aspx/460/using-url-rewrite-module/ ) is worth looking at. 如果您正在运行IIS7,那么(对于Webforms和MVC),URL重写模块( http://learn.iis.net/page.aspx/460/using-url-rewrite-module/ )值得关注。

Supports pattern matching and regular expressions for redirects, state whether they're temporary or permanent, plus they're all managable through the console. 支持模式匹配和正则表达式以进行重定向,说明它们是临时的还是永久的,并且都可以通过控制台进行管理。 Why code when you don't have to? 为什么在不需要时进行编码?

I'm presuming you need a common pattern here and not just a once off solution. 我假设您在这里需要一个通用的模式,而不仅仅是一次性解决方案。 ie you'll need it to work for 12345 & 12346 & whatever other IDs as well. 也就是说,您将需要它来用于12345和12346及其他任何ID。 You're probably looking for URLRedirection and applying a Regular Expression to identify a source URI that will redirect to your Target URI 您可能正在寻找URLRedirection并应用正则表达式来标识将重定向到目标URI的源URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM