Windows 服务器重定向问题

发布于 2024-10-01 14:14:01 字数 315 浏览 5 评论 0原文

我正在处理一些使用 Windows 服务器的客户端,因此不支持 .htaccess 文件。这不是什么大问题,但我担心的是:

我在 .htaccess 文件中设置了一条规则,将网站的非 www 版本重定向到 www 版本。这使得 URL 看起来更漂亮,并防止重复内容被索引。

然而,似乎没有一种简单的方法可以在 Windows 服务器上执行此操作。我已经阅读了有关设置 web.config 文件的教程,但是,我的 Windows 服务器经验非常有限,很多时候我只能通过 FTP 访问该站点(没有服务器访问权限)。

关于我可以使用的快速且相当简单的解决方案有什么想法吗?

I am dealing with some client's that use Windows servers and as such do not support .htaccess files. This is not a huge deal but, my concern is this:

I have a rule set up in my .htaccess file to redirect the non-www version of the site to the www version. This makes the URL's look nicer and prevents duplicate content being indexed.

However, there does not seem to be a simple way to do this on a Windows server. I have read through tutorials on setting up a web.config file but, my Windows server experience is very limited and many times I only have FTP access to the site (no server access).

Any ideas on a quick and fairly simple solution, that I could use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2024-10-08 14:14:01

使用以下内容创建 web.config(在根目录中)文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.domain.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

URL 重写模块 (必须安装至少 2.0 版)。

Create web.config (in the root directory) file with the next content:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.domain.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

The URL Rewrite Module (at least version 2.0) has to be installed.

如梦 2024-10-08 14:14:01

要使用自定义模块,请在文件夹 App_Code 中创建文件 CanonicalRedirectModule.cs,该文件夹是网站应用程序的根文件夹,其中包含以下内容:

using System;
using System.Web;

public class CanonicalRedirectModule : IHttpModule
{
    public const string configKeyCanonicalHostName = "CanonicalHostName";

    private string configCanonicalHostName;

    #region IHttpModule Members

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        this.configCanonicalHostName = System.Configuration.ConfigurationManager.AppSettings[configKeyCanonicalHostName];

        if (string.IsNullOrEmpty(this.configCanonicalHostName))
        {
            System.Diagnostics.Trace.TraceWarning("Can't find application setting {0} in configuration file (/configuration/appSettings/add/...).", configKeyCanonicalHostName);
//#if !DEBUG
//            return;
//#endif
        }

        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    #endregion

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        if (app.Request.Url.Host != this.configCanonicalHostName) //&& !app.Request.IsLocal
        {
            UriBuilder newUrl = new UriBuilder(app.Request.Url);
            newUrl.Host = this.configCanonicalHostName;

            app.Response.Redirect(newUrl.ToString(), true);
        }
    }
}  

然后在 web.config< 中配置模块/代码>:

 <?xml version="1.0"?>

<system.web>
  <!-- Configuration for classic pipeline mode -->
  <httpModules>
    <add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
  </httpModules>
</system.web>

<system.webServer>
  <!-- Configuration for integrated pipeline mode -->
  <validation validateIntegratedModeConfiguration="false" />
  <modules runAllManagedModulesForAllRequests="true" >
    <add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
  </modules>
</system.webServer>

To use custom module create file CanonicalRedirectModule.cs in folder App_Code it the root folder of the Web Site Application with next content:

using System;
using System.Web;

public class CanonicalRedirectModule : IHttpModule
{
    public const string configKeyCanonicalHostName = "CanonicalHostName";

    private string configCanonicalHostName;

    #region IHttpModule Members

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        this.configCanonicalHostName = System.Configuration.ConfigurationManager.AppSettings[configKeyCanonicalHostName];

        if (string.IsNullOrEmpty(this.configCanonicalHostName))
        {
            System.Diagnostics.Trace.TraceWarning("Can't find application setting {0} in configuration file (/configuration/appSettings/add/...).", configKeyCanonicalHostName);
//#if !DEBUG
//            return;
//#endif
        }

        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    #endregion

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        if (app.Request.Url.Host != this.configCanonicalHostName) //&& !app.Request.IsLocal
        {
            UriBuilder newUrl = new UriBuilder(app.Request.Url);
            newUrl.Host = this.configCanonicalHostName;

            app.Response.Redirect(newUrl.ToString(), true);
        }
    }
}  

Then configure module in web.config:

 <?xml version="1.0"?>

<system.web>
  <!-- Configuration for classic pipeline mode -->
  <httpModules>
    <add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
  </httpModules>
</system.web>

<system.webServer>
  <!-- Configuration for integrated pipeline mode -->
  <validation validateIntegratedModeConfiguration="false" />
  <modules runAllManagedModulesForAllRequests="true" >
    <add name="CanonicalRedirectModule" type="CanonicalRedirectModule"/>
  </modules>
</system.webServer>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文