{"id":4100,"date":"2022-09-20T09:44:04","date_gmt":"2022-09-20T16:44:04","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=4100"},"modified":"2022-09-20T09:44:05","modified_gmt":"2022-09-20T16:44:05","slug":"how-to-redirect-www-to-non-www-in-apache-with-htaccess","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=4100","title":{"rendered":"How to Redirect www to non-www in Apache with .htaccess"},"content":{"rendered":"\n<p>Sometimes you may need to redirect www URLs to non-www URLs for your website for SEO benefits. Here\u2019s how to redirect www to non-www in Apache htaccess. You can use these steps to ensure that there is only one version (non-www) of your website available online.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Redirect www to non-www in Apache htaccess<\/h2>\n\n\n\n<p>Here are the steps to redirect www to non-www in Apache htaccess file.&nbsp;Please ensure that you have enabled mod_rewrite in your Apache web server configuration. Only then your htaccess configuration will be applied by Apache server.<\/p>\n\n\n\n<p>If you have enabled htaccess using mod_rewrite, you can skip to step 4.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Enable mod_rewrite<\/h2>\n\n\n\n<p>Open terminal and run the following command to enable mod_rewrite on Ubuntu\/Debian systems. It is already enabled in CentOS\/Redhat systems.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo a2enmod rewrite\n<\/pre>\n\n\n\n<p>If mod_rewrite is already enabled, you will see an alert message.<\/p>\n\n\n\n<p>Restart Apache web server<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart apache2\n<\/pre>\n\n\n\n<p>Bonus Read :&nbsp;<a href=\"http:\/\/ubiq.co\/tech-blog\/redirect-url-another-url\/\">How to Redirect URL to Another URL<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Enable .htaccess in Apache Server<\/h2>\n\n\n\n<p>By default, Apache does not allow the use of .htaccess file. So open the default Apache server configuration file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo vi \/etc\/apache2\/sites-available\/000-default.conf<\/pre>\n\n\n\n<p>Add the following lines just before&nbsp;<em>&lt;\/VirtualHost&gt;<\/em>&nbsp;line.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Directory \/var\/www\/html&gt;\n   Options Indexes FollowSymLinks MultiViews\n   AllowOverride All\n   Require all granted\n&lt;\/Directory&gt;\n<\/pre>\n\n\n\n<p>Restart Apache web server<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart apache2\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Create .htaccess file<\/h2>\n\n\n\n<p>Open terminal and create .htaccess file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo vi \/var\/www\/html\/.htaccess\n<\/pre>\n\n\n\n<p>Make sure to add the following line at the top of your .htaccess file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine on<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Redirect www to non-www in Apache htaccess<\/h2>\n\n\n\n<p>Let\u2019s say you want to redirect all URLs from&nbsp;<em>www.example.com<\/em>&nbsp;to&nbsp;<em>example.com<\/em><\/p>\n\n\n\n<p>Add the following lines in your htaccess file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteCond %{HTTP_HOST} ^www.example.com [NC]\nRewriteRule ^(.*)$ http:\/\/example.com\/$1 [L,R=301]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may need to redirect www URLs to non-www URLs for your website for SEO benefits. Here\u2019s how to redirect www to non-www in Apache htaccess. You can use these steps to ensure that there is only one version (non-www) of your website available online. How to Redirect www to non-www in Apache htaccess <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=4100\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[104,5],"tags":[1146,1147],"class_list":["post-4100","post","type-post","status-publish","format-standard","hentry","category-apache","category-linux","tag-htaccess","tag-redirect-www-to-non-www-in-apache"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4100"}],"version-history":[{"count":2,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4100\/revisions"}],"predecessor-version":[{"id":4102,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/4100\/revisions\/4102"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}