You will receive a domain when you first host your website that can be accessed through both the WWW and non-WWW versions of your domain.
You might have purchased the domain name example.com and used it to host a website.
Using either www.example.com or example.com to navigate this website will take you to the same place.
These both appear the same to the end user and have no effect on how the website works.
Why You Must Redirect WWW to Non-WWW?
Well, a simple preference could be one cause. There is no right or wrong in this; some users or developers choose to have their website available in only one format.
Personally, I prefer not to have the www subdomain before the domain when viewing my own website.
In addition, SEO is a factor.
Search engine optimization, or SEO, is essentially a method by which content-crawling bots from search engines like Google crawl your website.
That is how they are able to display the search results when someone conducts an appropriate Google search.
Bots will occasionally find the www domain and occasionally the non-www domain when they search your website.
As a result, there are a lot more crawled requests and pages, and sometimes the right page suddenly disappears.
Google’s SEO specialists advise that even though they treat both URLs equally, you should have everything referring to one URL, regardless of which one it is.
You have the option of configuring your website to redirect to non-WWW or to always go to the WWW. Thus, consistency will be attained.
Now that we know why it’s crucial to redirect WWW to non-WWW or the other way around, let’s look at how we may accomplish this.
1. Redirect WWW to Non-WWW via Web.Config
If you have hosted or published an ASP.NET website, and you want to redirect your www website calls to non-www calls, we will write a redirect rule in the Web.Config and that will handle our redirects when a call comes to the server.
Add the redirect rules in the <rewrite><rewrite>
section of <system.webServer>
section in the web.config.
<rewrite>
<rules>
<rule name="NonWWWtoWWW" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mywebsite\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.mywebsite.com/{R:0}" />
</rule>
</rules>
<rewrite>
Let’s understand the above configuration:
The <rewrite>
section can contain <rules></rules>
section to add one or more rules to redirect or rewrite URLs. It can also contain <rewriteMaps></rewriteMaps>
to map old URLs to new URLs.
The <rules>
section can contain one or more rules for url rewrite/redirect. The section is used to define the logic of what to compare or match the request URL with, and what to do if the comparison is successful.
The <rule name="NonWWWtoWWW" stopProcessing="true">
defines the rule that will redirect users from your non-www version of the website to the “www” version. An attribute called “name” describes a rule’s name. When a rule action is taken, the stopProcessing property defines whether or not to process further rules. If the incoming URL matches the stated criteria and the redirect action is carried out in this case, stopProcessing=”true” will halt processing of the subsequent rules (if any).
A match, conditions, and action section are present in every rule. The regular expression or wildcard pattern that is used to match URL strings can be specified in the match> node.
The <match url=".*" ignoreCase="true" />
matches every URL to which this rule will apply. All URLs are accepted by the wildcard pattern.*.
The extra logical processes that should be carried out if a URL string meets the rule pattern are specified in the optional <conditions>
section. Here, you may look for specific HTTP header values or server variables.
<add input="{HTTP_HOST}" pattern="^mywebsite\.com$" />
specifies the HTTP_HOST is a server variable that points to your domain name e.g. http(s)://host:port/path?querystring
The pattern to compare is specified by the pattern property. Check if the URL is mywebsite.com without the www. by using the pattern "mywebsite\.com$".
The action stated in the <action>
section will be carried out if this condition is satisfied; else, the following rule will be applied.
The <action type="Redirect" redirectType="Permanent" url="https://www.mywebsite.com/{R:0}" /<
specifies to redirect a request to a new URL https://www.mywebsite.com/{R:0}
with the status code 301 (permanent redirect). {R:0}
is the back-references to rule patterns that returns matched string.
Thus, the above rule will redirect any non-www URL of http(s):///mywebsite.com
to https://www.mywebsite.com
.
2. Redirect WWW to Non-WWW via Plesk Control Panel.
It is also pretty simple to redirect www to non-www via Plesk control panel. Here are few steps and you can follow it
1. Login to your Plesk control panel
2. Find your website.
3. Click on hosting settings -> tick your preferred domain as shown below
Conclusion
In above article, you have learned how to redirect www to non-www and hope that article helpful for you. If you want to redirect http to https, please kindly feel free to read my tutorial on this blog. If you find this post helpful, please feel free to share it. Thank you. Happy coding!