【问题标题】:Redirect from non-www to www using 301 on IIS在 IIS 上使用 301 从非 www 重定向到 www
【发布时间】:2016-10-14 20:26:02
【问题描述】:

我已在1&1 的 Windows 服务器上托管我的网站。 在那里,不可能在 web.config 上添加元素 <rewrite><rules>... 所以我使用的是 .htaccess 文件... 我不是那么专家,我希望它只适用于 Apache 服务器。我错了!我的 .hataccess 也适用于 IIS。

代码如下:

//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>

重定向是使用 302 完成的问题。我想要一个 301 重定向。我正在使用提琴手,结果如下:

我遇到问题的网站是www.renovahaus.it 我该如何解决我的问题?

感谢

【问题讨论】:

    标签: .htaccess redirect iis http-status-code-301 http-status-code-302


    【解决方案1】:

    这通常使用 URLRewrite 模块完成,在 1&1 的情况下,这在 web.config 中被禁用,但是当您使用 .htaccess 文件时,.htaccess 的内容仍会转换为底层 web.config。

    您可以阅读关于转换过程的精彩教程here

    但总结是您可能正在寻找代码块:-

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

    应该将以下内容透明地添加到 web.config 文件中(即使您不能直接编辑它)

    <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>
    

    【讨论】:

    • 但为什么 .htaccess 文件重定向使用 302 而不是 301?
    猜你喜欢
    • 1970-01-01
    • 2013-05-06
    • 2018-05-01
    • 1970-01-01
    • 2010-11-27
    • 2021-12-03
    • 2017-05-03
    • 2014-04-09
    • 2018-10-19
    相关资源
    最近更新 更多