Append www in URL for Hosted ASP Site (i.e. no IIS

2019-04-14 14:31发布

I see many similar questions written in gibberish that I don't understand:

I'd like to know how to do this using Microsoft's technology ...or just explain to me what those others are talking about and how to use them.

Basically, if someone types "mydomain.com" into the address bar, I want it to resolve to "www.mydomain.com" when the page has finished loading.

EDIT: This is a hosted website, so I can't configure the IIS Server.

标签: asp.net html url
2条回答
混吃等死
2楼-- · 2019-04-14 15:19

This is kind of a funny solution. I am only suggesting it because of the limitations which you described. It's better to do it in IIS or using an HTTP Module like those other answers are suggesting. However, this would also work, it's just not a very good way to accomplish it.

You can put this code into your Page Init Event handlers (or Init Handler for your master page).

If Request.RawUrl.StartsWith("http://mydomain") Then
  Response.Redirect(Request.RawUrl.Replace("://", "://www."))
End If

Where mydomain is like mydomain.com without the www.

It checks to see if the URL doesn't have WWW where WWW "should" be. If it isn't there, redirects the user to a version of that page that has WWW in the correct spot.

查看更多
萌系小妹纸
3楼-- · 2019-04-14 15:30
  1. Non www to www redirect
  2. www.yourdomainname.com/default.aspx to www.yourdomainname.com

Now add config tag in web.config

<system.webServer>
<rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^yourdomainname.com$" />
          </conditions>
          <action type="Redirect" url="http://www.yourdomainname.com/{R:0}" redirectType="Permanent" />
        </rule>

        <rule name="Default Document" stopProcessing="true">
          <match url="(.*?)/?default\.aspx$" />
          <action type="Redirect" url="{R:1}/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

(or) Go with this one:

<rewrite>
    <globalRules>
        <rule name="Redirects to www.domain.com" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^domain.*(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^(www.)?mydomain2.(com|net)$" />
                <add input="{HTTP_HOST}" pattern="^www.domain.net$" />
            </conditions>
            <action type="Redirect" url="http://www.domain.com/{R:0}" />
        </rule>
    </globalRules>
</rewrite>
查看更多
登录 后发表回答