Cum să redirecționați un domeniu către versiunea non-WWW utilizând .htaccess

Pe această pagină
  1. Before You Begin
  2. Redirect WWW to Non-WWW
  3. How It Works
  4. Redirect WWW to Non-WWW with HTTPS
  5. Testing the Redirect
  6. Common Issues
  7. Summary

If you want visitors to always access your website without the www prefix, you can configure a permanent redirect in the .htaccess file.

For example:

https://www.yourdomain.com

will automatically redirect to:

https://yourdomain.com

This helps maintain a consistent URL structure and can improve SEO by preventing duplicate content issues.

Before You Begin

The .htaccess file is typically located in your website’s root directory:

public_html

If the file does not exist, you can create it manually.

Important: Before editing .htaccess, create a backup of the file in case you need to restore the previous configuration.

Redirect WWW to Non-WWW

Add the following rules to your .htaccess file:

# Redirect www to non-www

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

How It Works

  • RewriteEngine On enables URL rewriting.
  • RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] checks whether the requested hostname starts with www.
  • RewriteRule redirects the visitor to the same URL without the www prefix.
  • R=301 creates a permanent redirect.

Redirect WWW to Non-WWW with HTTPS

If your website uses an SSL certificate, it is recommended to redirect visitors directly to the secure HTTPS version:

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

Example:

https://www.yourdomain.com/page

becomes:

https://yourdomain.com/page

Testing the Redirect

After saving the .htaccess file:

  1. Open your website using the WWW version.
  2. Verify that the browser automatically redirects to the non-WWW address.
  3. Check several pages and subpages to ensure the redirect works correctly throughout the website.

Common Issues

If the redirect does not work:

  • Verify that the .htaccess file is located in the correct directory.
  • Check for conflicting redirect rules.
  • Clear your browser cache before testing again.
  • Ensure that URL rewriting is enabled on the server.

Summary

To force visitors to use the non-WWW version of your website, add the following rule to the .htaccess file located in your website’s root directory:

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

If your website uses SSL, replace http:// with https:// to ensure visitors are redirected to the secure version of the site.

Nu ai găsit răspunsul?

Echipa noastră de asistență este aici pentru tine 24/7 – scrie-ne și te ajutăm să rezolvi problema.