James Hay
James Hay

Reputation: 7325

URL Shortener is not previewing the target page

I'm creating an internal (links only from our site) URL shortening service. When I use a service like bit .ly or tinyurl and then post the shortened link to facebook, the preview of the target (full link) is displayed. When I try to do this with my own page, it displays the redirection page.

For example http://youtu.be/2323 would map to http://www.youtube.com/watch?v=123456, but my link

http://exam.pl/2323 will show http://exam.pl/Redirect.aspx instead of the actual page in the database. Do I need to the redirection on the server itself or something?

Thanks

UPDATE: Solved with an HttpHandler like in the answer below. I changed the response because apparently Response.Redirect automatically sends a 302 status whereas 301 is more correct.

context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", httplocation);
context.Response.End();

Upvotes: 1

Views: 197

Answers (1)

Dustin Davis
Dustin Davis

Reputation: 14585

I recommend using an http handler instead of an actual page to do the redirect http://support.microsoft.com/kb/308001

I also recommend you provide a proper 301 http status http://en.wikipedia.org/wiki/HTTP_301

Update: (This is purley from memory and may not compile as is)

public class IISHandler1 : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string url = content.Request.Url.ToString();
            string newUrl = Translate(url);
            context.Response.ResponseCode = 301;
            context.Response.Redirect(newUrl);
        }

    }

You modules get processed AFTER handlers so you should handle the request in a handler. If not possibly to handle then just ignore it and let it pass through

Upvotes: 1

Related Questions