A redirect is a mechanism that automatically sends a visitor from one URL to another. When a web server receives a request for a specific resource, instead of delivering it, it informs the browser to navigate to a specified new address. This is particularly important when changing a website's structure, or domain name, or preserving SEO traffic during a migration to a new resource.
Domain forwarding may be needed in various scenarios:
There are several methods to configure redirects. The choice depends on your resources and available tools:
.htaccess
file (relevant for hosting based on Apache);Below are some common methods explained in detail.
In the virtual host configuration file (/etc/apache2/sites-available/example.conf
), add the following rule:
ServerName old-domain.com
Redirect 301 / http://new-domain.com/
This code will redirect all requests from old-domain.com
to new-domain.com
using a 301 status code, indicating a permanent move.
In the site configuration file (e.g., /etc/nginx/sites-available/example
), add the following:
server {
listen 80;
server_name old-domain.com;
return 301 http://new-domain.com$request_uri;
}
This setup redirects all requests from old-domain.com
to new-domain.com
, preserving the path and query parameters.
Restart the server after making changes:
sudo service apache2 restart
sudo service nginx restart
The .htaccess
file is a configuration file for Apache that allows you to define redirect rules without accessing the primary configuration files. This is especially convenient for shared hosting.
Example of domain forwarding using .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain.com [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [L,R=301]
This code checks if the requested domain is old-domain.com
and redirects it to new-domain.com
. The 301 status code informs search engines that the move is permanent.
Many hosting providers offer user-friendly interfaces for setting up redirects:
This method is particularly easy as it eliminates the need for manual editing of configuration files. Once the changes are saved, the control panel automatically writes the necessary rules for you.
Setting up redirects for domain forwarding is a crucial step to maintaining website rankings in search engines and ensuring a smooth user experience during changes to site structure or domain name. You can use web server configuration files, the .htaccess
file, or a hosting control panel. The choice of method depends on your server access and personal preferences. A properly configured redirect guarantees uninterrupted and reliable website operation in the long term.
Learn more about how to purchase a domain, hosting, VPS, or dedicated server.
A 301 redirect is used for permanently redirecting users and search engine crawlers from one URL to another. This is necessary when a page has been moved to a new address, and you want to retain its authority and ranking. A 301 redirect signals to search engines that the old URL is no longer valid and transfers all its value to the new URL.
Yes, DNS can be used for URL forwarding. This is known as a DNS redirect, and it allows you to forward all traffic from one domain to another. However, DNS redirects are generally used for redirecting entire domains rather than individual pages. For more precise redirects at the page level, 301 redirects are typically used.
A 301 redirect indicates a permanent move, meaning the page has been relocated permanently to a new URL. A 302 redirect denotes a temporary move, implying the page may return to the original URL in the future. A 301 redirect transfers the full authority and ranking of the old URL to the new one, whereas a 302 redirect does not transfer the full value as it is considered temporary.