How to make a redirect from www to without www

I made a redirect with this code in the file htaccess:

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

BUT!

Now if I go to any page with www, then I will be redirected to http://example.ru/index.php and thrown 404.

How do I make it so that when I go to any page with www, I get thrown to a similar page without www?

Author: Alex, 2016-11-26

2 answers

Problem

Search engines count sites http://yoursite.ru/ and http://www.yoursite.ru/ completely different. It follows that if some of the sites will link to http://yoursite.ru/, and the other part on http://www.yoursite.ru/, then the link popularity of your site is divided between different domains, which is not good. Something has to be done about it.

Solving the problem

Using a 301 redirect, which will automatically be forward everyone who follows a link starting with http://yoursite.ru/ to http://www.yoursite.ru/, you can let the search engines know that these 2 domains are like one.Thus, the link popularity will no longer be distributed between the two domains, but will be considered common, which will improve the site's position in search engines.

Installing a 301 redirect of a domain without WWW to a domain with WWW on an Apache server

  1. Before first of all, make sure that the ModRewrite module is active on your Apache server. Basically, it is active by default, but it will not hurt to make sure once again in order to avoid problems in the future. If this module is inactive, just contact the support service of your hosting provider and explain the situation to them. There should be no problems when activating this module.

    If you have access to the httpd.conf file, you can activate this module yourself. To do this, open this file is for editing (do not forget to make a backup copy of it first) and uncomment the following line:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    After you uncomment this line, save the file and restart the server to continue working further.

  2. Download the file to your computer from the server .htaccess

  3. Make a backup copy of this file and save it, if possible, in another place on your computer.

  4. Open the original file .htaccess for editing, for example, in notepad.

  5. Now add the following lines to it, but be careful and don't forget to replace example.ru to the address of your site

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.ru
    RewriteRule (.*) http://www.example.ru/$1 [R=301,L]
    

    And vice versa, who needs to redirect a domain from WWW to a domain without WWW:

     RewriteEngine On
     RewriteCond %{HTTP_HOST} ^www.example.ru$ [NC]
     RewriteRule ^(.*)$ http://example.ru/$1 [R=301,L]
    
  6. Save the changes to the file and upload it to the server.

  7. After that, open the browser and enter the address of your site in the address bar without WWW, for example http://example.ru/ and go to the site. If you did everything correctly, then the browser's address bar will automatically change - the site address will change from the one you entered to http://www.example.ru/.

  8. But to be 100% sure of the success of the work done, I advise you to go here, enter the site address without WWW. This tool will show you whether the 301 redirect you configured works or not. If you did everything right, then you should see the following (pay special attention to the highlighted bold sections of the code):

№ 1 Server Response: http://example.ru

HTTP Status Code: HTTP/1.1 301 Moved Permanently

Date: Wed, 14 Mar 2007 22:49:28 GMT

Server: Apache/1.3.27 (Unix) PHP/4.4.1 FrontPage/5.0.2.2510 mod_ssl/2.8.14 OpenSSL/0.9.6b

Location: http://www.example.ru/

Connection: close

Content-Type: text/html; charset=iso-8859-1

Redirect Target: http://www.example.ru/

№ 2 Server Response: http://www.example.ru/

HTTP Status Code: HTTP/1.1 200 OK

Date: Wed, 14 Mar 2007 22:49:28 GMT

Server: Apache/1.3.27 (Unix) PHP/4.4.1 FrontPage/5.0.2.2510 mod_ssl/2.8.14 OpenSSL/0.9.6b

Connection: close

Content-Type: text/html

  1. If you have everything in the key (highlighted in bold) places, then everything works!

If you have completed the above steps and have not achieved the desired result, then use a backup copy of the file .htaccess – do all the above steps with it again, but be extremely careful. Check each step.

If it still doesn't work out, then contact your hosting provider's support service, explain the situation to them, and let them take appropriate measures aimed at solving your problem.

Source

 3
Author: spectre_it, 2020-10-04 05:17:00

Try this method:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
 0
Author: nikant25, 2016-11-27 11:08:44