How to redirect everything via Web.config or C # to https: // www. site version?

I have an ASP.NET site hosted on GoDaddy. I need to redirect (301) each request https://www.example.com/whatever

So for example:

This is a piece of cake on Linux or cPanel hosting. But I don’t know how to do it on GoDaddy (I asked for support, but they seem to know nothing ...). I think this is easy to do with IIS, but GoDaddy does not give you access to IIS.

So, is there a way to do this through Web.config or C #? Preferably web.config

+3
source share
2 answers

Check out this article, which contains several different options for preventing duplicate URLs on your ASP.NET site: Methods for preventing duplicate URLs in your Site . I have summarized the various methods below, but will review an article for more in-depth treatment, as well as a demo of ASP.NET.

Issuing persistent redirects from ASP.NET

You can check the incoming URL for each request in the event handler Global.asax Application_BeginRequest. If the URL is missing a host www., you can add it and make a permanent redirect to the new URL.

protected void Application_BeginRequest(object sender, EventArgs e)
{
   if (Request.Url.Authority.StartsWith("www"))
      return;

   var url = string.Format("{0}://www.{1}{2}",
               Request.Url.Scheme,
               Request.Url.Authority,
               Request.Url.PathAndQuery);

   Response.RedirectPermanent(url, true);
} 

URL- URL- IIS 7

IIS7 Microsoft URL Rewrite Module, Web.config. , GoDaddy, , IIS7 ( , ) URL. .

, , :

<configuration>
   ...

   <system.webServer>
      <rewrite>
         <rules>
            <rule name="Canonical Host Name" stopProcessing="true">
               <match url="(.*)" />

               <conditions>
                  <add input="{HTTP_HOST}" pattern="^yoursite\.com$" />
               </conditions>

               <action type="Redirect" url="http://www.yoursite.com/{R:1}" redirectType="Permanent" />
            </rule>
         </rules>
      </rewrite>
   </system.webServer>
</configuration>

- URL-, ISAPI_Rewrite. , GoDaddy , . .

, www., , , , URL.

URL, <link> <head> -. , : <head> -, , URL-:

<link rel="canonical" href="canonical_url" /> 

View/Source Stackoverflow.com, <link>. , URL- - , www.yoursite.com/foo.aspx yoursite.com/foo.aspx .

!

+5

! .

Application_BeginRequest(), 1) , 2) localhost 3) .

protected void Application_BeginRequest(object sender, EventArgs e)
{
    /**
     *  Note:
     *    Since Url.Authority always returns an all lowercase string, we can use 
     *    StringComparison.Ordinal (instead of OrdinalIgnoreCase) for www and 
     *    localhost checks.
     *  
     *  Ordinal rationale:
     *    "Use comparisons with StringComparison.Ordinal or 
     *    StringComparison.OrdinalIgnoreCase for better performance"
     *    see http://msdn.microsoft.com/en-us/library/dd465121.aspx#choosing_a_stringcomparison_member_for_your_method_call
     */
    if (Request.Url.Authority.StartsWith("www", StringComparison.Ordinal))
        return;

    /**
     * Avoid redirection on localhost.
     */
    if (Request.Url.Authority.StartsWith("localhost", StringComparison.Ordinal))
        return;

    /**
     * Because Uri.Authority is always lowercase, this code has the side-effect of 
     * enforcing a lowercase authority (e.g., http://NoisyKeys.com/About is 
     * converted to http://www.noisykeys.com/About).  This is asymmetric with 
     * previous conditionals.  To attain symmetry, we probably want to use 
     * Request.Url.OriginalString instead of Request.Url.Authority.
     */
    var url = string.Format("{0}://www.{1}{2}", 
        Request.Url.Scheme,
        Request.Url.Authority,
        Request.Url.PathAndQuery);

    Response.RedirectPermanent(url, true);
}
+1

Source: https://habr.com/ru/post/1792269/


All Articles