开发者

Is this a correct way of doing a 301 redirect?

开发者 https://www.devze.com 2022-12-21 16:36 出处:网络
When I check my page using those online host header analyzers, the page says 200 OK. But when viewing in my browser, it redirects to another website (which is how I want it to be).

When I check my page using those online host header analyzers, the page says 200 OK.

But when viewing in my browser, it redirects to another website (which is how I want it to be).

The code that I use is:

 context.Response.Status = "301 Moved Permanently";
 context.Response.AddHeader("Location", "http://www.example.com/article" + articleID);
 context.Response.End();

I put this code in a HttpModule.

it is working beca开发者_JAVA百科use when you try and hit the url, it does the redirect. It just doesn't seem to be returning the correct http header.

Is there something wrong?


Make sure the response buffer is completely clear before you add your headers:

context.Response.Clear();
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", "http://www.example.com/article" + articleID);
context.Response.End();


try:

context.Response.StatusCode = 301;
context.Response.StatusDescription = "Moved Permanently";
context.Response.RedirectLocation = "http://www.example.com/article" + articleID;
context.Response.End();

I use the above in a custom module and it does return a proper 301 HTTP response.


Your code is exactly correct. I've used exactly what you've got for years:

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


Could you try using: HttpContext.ApplicationInstance.CompleteRequest()

Instead of Response.End()?


When I use the HTTP logging in Web Development Helper, I see the 301 and the 200. So, yes, your code is correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号