Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 重定向自http://sitename.com 到http://www.sitename.com_Asp.net_Asp.net 4.0 - Fatal编程技术网

Asp.net 重定向自http://sitename.com 到http://www.sitename.com

Asp.net 重定向自http://sitename.com 到http://www.sitename.com,asp.net,asp.net-4.0,Asp.net,Asp.net 4.0,我正在使用ASP.NET4。我想从301重定向到 做这件事的最佳方式(和可选方式)是什么 我的网站也在ip地址上建立索引。我怎样才能阻止它。你可以在页面上做一个简单的检查 protected void Page_Load(object sender, EventArgs e) { string serverName = HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]); s

我正在使用ASP.NET4。我想从301重定向到

做这件事的最佳方式(和可选方式)是什么


我的网站也在ip地址上建立索引。我怎样才能阻止它。

你可以在
页面上做一个简单的检查

    protected void Page_Load(object sender, EventArgs e)
    {
        string serverName = HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]); 
        string filePath = Request.FilePath;
        if (!serverName.ToLower().StartsWith("www.")) 
            serverName = "www." + serverName; 
        Response.Redirect("http://" + serverName + filePath); 
    }
或者,您可以将以下内容添加到htaccess文件中:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] 

替换
domain.com
http://www.domain.com
与您的域一起使用。

我会在IIS级别执行301重定向,请参阅以下日志:


要停止在IP地址上对站点进行索引,请不要将其保留为空。

根据您的DNS提供商,他们可以设置重定向,通常称为web转发、web别名或web重定向。只要确保它是301而不是框架集

相关问题:


您可以使用url重写将此规则添加到规则标记


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

这个密码对我有用。将此添加到web.config文件的规则部分

Response.Redirect发出302重定向,而不是301重定向。.htaccess选项只适用于Apache,而不适用于IIS。你当然是对的……教我如何浏览OP想要的内容。我对IIS没有太多控制权。如果您使用的是IIS7,并且托管提供商为您提供了正确的权限,您可以在system.webServer部分的Web.config文件中配置它。是的,但是url重写模块需要安装在服务器上,而且看起来不是。这就是为什么我需要一个替代品
<rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^yoursite.com$" />
          </conditions>
          <action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
        </rule>