How to 301 redirect old domain to new domain in .htaccess

If you change the domain name, then in order to transfer the weight of the original domain name to the new domain name, the best way is to use 301 redirection. We can use The oldTo the new domain name, the specific steps are as follows:

Create a .htaccess file, or write the following code into an existing .htaccess file. It will ensure that all directories or web pages of the old domain name are correctly redirected to the new domain name.

RewriteEngine on RewriteRule (.*) https://uzbox.com/$1 [R=301,L]
Please change the boxpu.com above to the domain name you want to redirect to.

Remember that the .htaccess file must be placed in the root directory of the old website, and the new website must maintain the same directory structure and web page files as the old website.

In addition, I suggest that you summarize the external links of the old website and contact the corresponding site to modify the URL of the imported link to point to the new site.

Note: .htaccess files only work on Linux hosts that use Apache with the Mod Rewrite module installed as the web server.

How to 301 redirect old domain to new domain in .htaccess

.htaccess files are commonly used configuration files on Apache servers, used to manage website redirection, URL rewriting, access control and other functions.301 Redirect It is a permanent redirect, meaning that the URL of the page has changed permanently and search engines will pass the weight of the old link to the new one.

The following is the use .htaccess File Implementation 301 Redirect Several common methods:

1. Single
If you want to redirect a specific page to another page, you can use the following code:

Redirect 301 /old-page.html http://www.example.com/new-page.html

- /old-page.html is the relative path to the old page you wish to redirect.
- http://www.example.com/new-page.html is the full URL of the new page you want the user to be redirected to.

2. Redirect the entire website to the new domain
If you changed your domain and want to redirect your entire site to the new domain:

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

 

- ^old-domain\.com Indicates the old domain name.
- http://www.new-domain.com/$1 Redirects users to the new domain name, preserving the path of the original URL.

3. Redirect non-www to www
If you want to redirect all non-www requests to the www domain, you can use:

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

 

- ^example\.com Check if any user is accessing the version without www.
- http://www.example.com/$1 Redirects the user to the domain with www, while preserving the request path.

 4. Redirect www to non-www
If you want to redirect all requests with www to the domain without www, you can use:

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

 

- ^www\.example\.com Matches domain names with www.
- http://example.com/$1 Redirects the user to the domain name without the www, while preserving the path.

5. Redirect HTTP to HTTPS
Force redirection of all HTTP requests to HTTPS for increased security:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

 

This code will redirect all non-HTTPS requests to the HTTPS version.

6. Directory Redirection
If you want to redirect a directory to a new directory:

Redirect 301 /old-folder/ http://www.example.com/new-folder/

 

- /old-folder/ is the old directory path.
- http://www.example.com/new-folder/ is the full URL of the new directory.

use .htaccess It is very simple to do a **301 redirect** with the ./html file, and common uses include page redirects, domain migrations, redirects between www and non-www, and HTTP to HTTPS redirects. Make sure the path, domain, and protocol are correct when applying redirects to avoid circular redirects or incorrect URLs.

3/5 - (4 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *