How to add "www." to a website with web.config?

Asked

Viewed 60 times

0

I do not have much knowledge in regular expressions and I am trying to identify if when accessing the site the user typed www before the name of the site.

In case he hasn’t typed, I have to add. But I’m having a little trouble doing this on the Asp web config. I tried something like this:

<rule name="teste">
  <match url="^([^w]\w*)">;
  <action type="Rewrite" url="www.{R:1}">
</rule>

2 answers

2


Following the logic of this other answer I made Remove "www" from domain by forwarding 301, the difference is that you have to "change" the action type="Redirect" for add input:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Adiciona prefixo WWW">
                <match url="(.*)" ignoreCase="true" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^MEUDOMINIO\.com" />
                </conditions>
                <action type="Redirect" url="http://www.MEUDOMINIO.com/{R:1}" redirectType="Permanent" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

1

I ended up doing it in a similar way. Follow the example for those who need:

<rule name="Add WWW" stopProcessing="true">
  <match url="^(.*)$" />
  <conditions>
     <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
  </conditions>
  <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.