Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,794 members, 7,820,789 topics. Date: Tuesday, 07 May 2024 at 09:39 PM

How To Redirect A Site To Non-www And To WWW - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How To Redirect A Site To Non-www And To WWW (690 Views)

How To Redirect Domain Name / Redirect A Page In Php? / 5 Lies You Need To STOP Telling Yourself As A Site Owner (2) (3) (4)

(1) (Reply)

How To Redirect A Site To Non-www And To WWW by todhost(m): 3:55am On Sep 26, 2015
The following tutorial will show you how to redirect your site's domain from non-www to www or vice versa. Redirecting one to the other is useful for SEO. Technically reffered to as canonical urls, it helps resolve your domain name to one url.

If you go to https://todhost.com, you'll see the URL changes a bit once you click enter. It goes to the www version. In order words, it goes to https://www.todhost.com. It's a subtle change and it's usually not noticed by users.

So why have they done this? It's because, technically, the two are different domains but they have the same content. Without the redirect, they end up with duplicate content. With the redirect, they only have one site, so only one set of content.

With only one set of content, they guarantee they won't be penalized by search engines or have their ranking split.
Using The .htaccess File

To move your site to www, find your .htaccess file using your host's file manager or an FTP client.

Within it, look for the following code:

RewriteEngine On

If the code isn't there, insert it at the top of your file.

Then, to redirect non-www to www, use the following code directly below RewriteEngine On:

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

For www to non-www, use:

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

If you're on a Windows IIS server, the above code won't work for you.
On an IIS server

On a Windows (IIS) server, find your web.config file and make the following rule changes to your rules tag:

Redirect non-www to www


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


Redirect www to non-www


<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to non-www" stopProcessing="true">
<match url=".*" >
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain\.com$" />
</conditions>
<action type="Redirect" url="http://yourdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>


Afterward, check your site, including some internal pages, and try different www and non-www combinations of your domain to see if the redirect is working as expected.

More: https://www.todhost.com/blog/

(1) (Reply)

/ Free Sponsored Posts / Get Etisalat 1GB Internet Bundle For N500 Only

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.