Simone
Simone

Reputation: 2430

Redirect from non-www to www using 301 on IIS

I have hosted my website on a windows server on 1&1. There, is not possible add on the web.config the the elements <rewrite><rules>... So I am using an .htaccess file... I am not so expert, I expected it would work just on Apache server. I was wrong! My .hataccess works also on IIS.

Here the code:

//Rewrite to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]

//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

The problem the redirect is done using a 302. I want a 301 redirect. I am using fiddler and this is the result:

enter image description here

The website where I have the problem is www.renovahaus.it How can i solve my problem?

Thanx

Upvotes: 0

Views: 346

Answers (1)

nyxthulhu
nyxthulhu

Reputation: 9752

This is normally done with the URLRewrite module, in the case of 1&1 this is disabled in the web.config however when you use a .htaccess file the contents of the .htaccess are still translated to the underlying web.config.

You can read a great tutorial on the conversion process here

But the summary is you are probably looking for the code block :-

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Which should transparently add the following to the web.config file (Even though you can't edit it directly)

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example\.com$" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        <add input="{URL}" pattern="^/favicon.ico$" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="index.php?q={R:1}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite>

Upvotes: 0

Related Questions