Home Linux ServerWeb Server Instructions to redirect www to non-www and vice versa with .htaccess

Instructions to redirect www to non-www and vice versa with .htaccess

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

Usually, you use www.domain, but in some cases, if you have not set up or pointed the domain IP, it will not take effect. In this article, AZDIGI will guide you to use www and no www with a .htaccess file

1. Use www for domain

To use www.domain, you must point www to the hosting IP with an A record; by default, www is also a sub. You open the .htaccess file and add the following code

Note: Replace mydomain with your domain

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

2. On the contrary, if you don’t want www

For a shorter URL for easy brand recognition and think about removing www.domain.

You open the .htaccess file and add the following code

Note: Replace mydomain with your domain

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

Additional information: In both cases, the 301 parameter is very important, it means permanent redirect, so do not leave this parameter out.

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign