When it comes to redirecting traffic from one website or domain to another, it’s important to decide on a few factors first.
Is this a permanent redirect or temporary?
If you only intend on directing traffic to the new URL temporarily, until it returns back or changes again, you may consider one of the following types of redirects:
302 Found (or Moved Temporarily): This is a temporary redirect that tells search engines and web browsers that the requested resource has been temporarily moved to a new URL. This is often used for short-term changes or testing, and the original URL is expected to be available again in the future.
307 Temporary Redirect: This is similar to the 302 status code but is more specific in its intended usage. This redirect status code is used to indicate that the requested resource has been temporarily moved to a new URL, and the client should continue using the original URL for future requests.
If this is a permanent redirect, you may consider one of these redirects instead:
301 Moved Permanently: This is a permanent redirect that tells search engines and web browsers that the requested resource has been permanently moved to a new URL. This is the most common type of redirect used when a website or page is permanently moved.
308 Permanent Redirect: This is similar to the 301 status code but is more specific in its intended usage. This redirect status code is used to indicate that the requested resource has been permanently moved to a new URL, and the client should update its bookmarks and links to use the new URL for future requests.
Lastly, if this a different type of redirect, you may be looking for:
303 See Other: This redirect status code is often used in response to a POST request. It tells the client to issue a GET request to a new URL, which is typically the result of a form submission.
Apache or nginx?
To create a redirect in Apache,
the easiest way is to add a .htaccess file to the root document folder of the domain, and add the following content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^web.space$ [NC]
RewriteRule ^ https://new.web.space%{REQUEST_URI} [R=301,L]
In this configuration, we enable the RewriteEngine and define a RewriteCond directive that matches the HTTP_HOST header to the web.space domain using the regular expression ^unlimitedweb.space$. The NC flag specifies that the match should be case-insensitive.
The RewriteRule directive then matches any request URI using the regular expression ^, which matches the beginning of the URI. The REQUEST_URI variable contains the rest of the URI, which is then appended to the new URL https://new.web.space. This means that original folder and link structures will continue to work on the new domain. The R=301 flag specifies that a 301 redirect should be issued, and the L flag specifies that the redirect should be the last rule applied.
Note that in order to use an .htaccess
file for rewriting URLs, you need to ensure that the AllowOverride
directive is set to All
in the Apache configuration file for the corresponding directory. If AllowOverride
is set to None
, the directives in the .htaccess
file will be ignored.
Hint: LiteSpeed web server is compatible with Apache .htaccess
files, so you can use the same syntax and directives as you would with Apache. The mod_rewrite
module is also supported in LiteSpeed, so you can use regular expressions to match and redirect URLs as needed.
The create a redirect in nginx,
You can edit the nginx config file ( /etc/nginx/nginx.conf ), to add the following (in between server { } brackets):
if ($request_uri ~* "^/.*$") {
return 301 https://new.web.space$request_uri;
}
This is an Nginx configuration directive that matches any request URI that starts with a forward slash (/) and redirects it to a new URL using a regular expression.
Here is a breakdown of the configuration directive:
The if statement defines a condition that must be met for the directive to take effect. In this case, the condition is a regular expression match on the request_uri variable.
The ~* operator specifies that the regular expression match should be case-insensitive (~), and that it should match the entire request_uri string (*).
The regular expression ^/.$ matches any string that starts with a forward slash (^/) and is followed by zero or more characters (.), and ends with the end-of-line anchor ($).
The return directive specifies that the web server should issue a 301 Moved Permanently status code and redirect the request to a new URL. The new URL is constructed using the new.web.space domain and the original request_uri variable.
When someone tries to access https://web.space/my-article they will be redirected to https://new.web.space/my-article .