Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:42:33+00:00 2026-06-04T12:42:33+00:00

I am trying to implement URL redirect for the website rather than doing it

  • 0

I am trying to implement URL redirect for the website rather than doing it page by page. I want to do it in the global.asax file. Below is the code i have defined.

I want to have http://website.net as my main url & want to have a permanent URL redirect if someone types in http://www.website.net.

Unfortunately it is not working for the live website. Can anyone point out the problem in the code. The code doesn’t generate any error.

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:42:34+00:00Added an answer on June 4, 2026 at 12:42 pm

    Main problem: Your’re doing the above stuff in Application_Start – which is only executed once. You should hook up with each request. Try this:

    void Application_BeginRequest(object sender, EventArgs e) 
    {
        // Code that runs on every request
    
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
        }
    
    }
    

    An even better approach would be to use URL rewriting, which can be configured from within Web.Config:

    Microsoft rewriting module – Force www on url Or remove www from url

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to implement a custom error page, what i want to be able
I am trying to implement Fb registration in my site.I want to redirect it
I am trying to implement URL Routing for a non MVC application. Here's my
I'm trying to implement java like url pattern matching in php. Suppose i have
Am trying to implement scraping using nodejs. Am sending request to a particular url
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I'm trying to implement URL detection for TRichEdit component using EM_AUTOURLDETECT message. I have
i am trying to implement pretty url. Everything i have written in PHP and
I'm trying to implement Approach 3 from this Url Rewriting article. I've added all
really need your help with log-in-with-facebook feature I'm trying to implement on my website.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.