开发者

Redirect from http://sitename.com to http://www.sitename.com

开发者 https://www.devze.com 2023-03-04 22:40 出处:网络
I am usi开发者_StackOverflow中文版ng ASP.NET 4. I want to do a 301 redirect from http://sitename.com to http://www.sitename.com.

I am usi开发者_StackOverflow中文版ng ASP.NET 4. I want to do a 301 redirect from http://sitename.com to http://www.sitename.com.

What's the best way (and optional ways) to do it?

My site is also getting indexed on the ip address. How can I stop that.


You could do a simple check in the Page_Load:

    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); 
    }

Or you could add the following to the htaccess file:

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

Replacing domain.com and http://www.domain.com with your domain.


I would do the 301 redirect at the IIS level, see this blog posting: SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7

To stop the site being indexed on the IP address, configure the "site" in IIS to have a host name, don't leave it blank.


Depending on your DNS provider it may be possible for them to set up redirects usually called something like web forwards, web alias or web redirects. Just make sure it's a 301 not a frameset.

Related questions: Canonical name redirects in Godaddy?


<rule name="Redirect to www">
  <match url="(.*)" />
  <conditions>
    <add input="{SERVER_PORT}" pattern="443" negate="true" />
  </conditions>
  <action type="Redirect" url="https://www.serdardemir.net/{R:1}" />
</rule>

you can use url rewrite add this rule to rules tag


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

This code worked me. Add this in rules section of your web.config file

0

精彩评论

暂无评论...
验证码 换一张
取 消