Basically there are a few options for the redirect and I don't think there is one best way, it depends on the use case.
Placement of the Redirect
Virtual Host
Prerequisite: You need administrative access to the Virtual Host Config.
If the old domain has no content at all and should be redirected completely, then the variant Virtual Host is recommended, since no DocumentRoot
must exist for the domain, thus no directory in the file system, if for the old domain anyway no more content has.
Disadvantage: For each change to the redirect, a reload or restart of the Apache web server must be performed.
.htaccess
If there is no access to the Virtual Host Config, e.g. when hosting with a shared hosting provider, there is the possibility to perform the redirect "natively" in the web server via the .htaccess
file.
This variant is also useful if changes to the redirect are to be made regularly or by users who should not have administrative access to the Apache web server.
Another advantage of the .htaccess
file variant is that the redirect can be changed (provided the domain is already configured correctly) without having to restart or reload the Apache web server.
The redirect via Apache web server
Redirect Directive (mod_alias)
The easiest way is to use the Redirect
directive from the mod_alias
module (the module must be enabled).
The syntax of the redirect directive is:
Redirect [status] [URL-path] URL
- Example
Redirect 301 /sub https://example-new1.com/
Redirect 301 / https://example.com/
- Example with Virtual Host
<VirtualHost *>
ServerName www.example-old.com
Redirect 301 /sub https://example-new1.com/
Redirect 301 / https://example.com/
</VirtualHost>
Redirect
can be used multiple times for different URL paths and redirects. Conditions for a redirect can be defined via Location
or LocationMatch
, more on this (including examples) in the Redirect reference.
There is also a RedirectMatch
directive, which works equivalent to the Redirect
directive, but supports regex matching.
RewriteRule (mod_rewrite)
More powerful over conditions is the redirect via RewriteRule
from the mod_rewrite
module (the module must be enabled).
The syntax of the RewriteRule directive is:
RewriteRule Pattern Substitution [flags]
- Example
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example-old.com
RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]
- Example with Virtual Host
<VirtualHost *>
ServerName www.example-old.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example-old.com
RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]
</VirtualHost>
Note that the rewrite engine must be explicitly enabled via RewriteEngine on
, despite the fact that the mod_rewrite
module is enabled on the server side.
Conditions can be defined via RewriteCond
, even several per RewriteRule
.
The flags of the RewriteRule
:
R=301
means that a redirect is performed, with HTTP status code 301
.
L
means that the rule processing will be stopped at this point, since a redirect is to be performed.
Rating
I don't know any measurements or statistics, but I think that the Redirect Directive in the Virtual Host is the fastest or most efficient variant, because
- the rewrite engine does not need to be loaded
- the VHost is cached when the Apache server is loaded and there is no need to search for a
.htaccess
file
For the SEO-friendliness, the variants make no difference, to the outside always takes place an SEO-friendly 301 redirect, so does not differ.