0

Which is the best way to do 301 redirects (for whole domains and also for specific pages/directories)? Using Apache virtual blocks, .htaccess or other solutions?

"Best" for me probably means the fastest and most SEO-friendly option, but maybe there are other important factors (security, easier to configure and maintain...).

Right now I'm using Apache, but for example for https apparently always checks the certificate of the old domain before redirecting to the new one, so maybe there are faster solutions? (I don't really know much about this subject, just asking)

Thanks!

3 Answers 3

2

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

  1. the rewrite engine does not need to be loaded
  2. 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.

1

Just to clarify -- is this a situation where using a CDN, for whatever reason, is completely off the table?

If not, I recommend Page Rules via Cloudflare's free plan, which is what I personally use to do this. They have an easy "forwarding URL" option where you can choose between 302 and 301. It is naturally much faster than anything most people could achieve by serving directly from a single server, as we're talking about a giant CDN with aggressive caching processing and serving these from the network edge. If you need many redirects handled this way, you can also use their Workers or/and Bulk Redirects.

0
RewriteCond %{HTTP_HOST} *!^www*.url\.com [NC]

RewriteRule (.*) http://www.url.com/$1 [L,R=301]]

The above two expressions tells apache to examine the host the visitor is accessing. The exclamation point (!) in front of www.url.com negates the comparison, saying, “If the host IS NOT www.url.om, then perform RewriteRule.” In our case RewriteRule redirects them to www.url.com while preserving the exact file they were accessing in a back-reference.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .