.htaccess, vhost or plugin: where to place your redirect plan?

CMS plugin, .htaccess file or server vhost: the three locations for a redirect plan do not use the same resources. Technical order of preference, maintenance, and the right choice for each need.

.htaccess, vhost or plugin: where to place your redirect plan?

Once the redirect plan is finished and the steps of the migration checklist have been followed, one question remains: where should it live? There are three options, a CMS plugin, .htaccess or the vhost, and they do not use the same resources at all. The right choice mostly depends on the case: a full redesign or a few URLs over time.

The three possible locations for a redirect plan

A CMS plugin or module

WordPress (Redirection, Yoast, Rank Math), PrestaShop, Magento or a SaaS such as Shopify all offer a dedicated module. Entries are stored in the database and managed from a back-office menu, with no code. It is the most accessible option for a marketing or SEO team.

The .htaccess file (Apache)

This is a configuration file read in each folder of the site. FTP or SSH access is enough to update it. You write the Redirect, RedirectPermanent, RedirectMatch or RewriteRule directives (mod_rewrite, with regular expressions), and they apply as soon as you save, without a restart. A syntax error, however, takes the whole site down with a 500 status.

Nginx has no per-directory equivalent: it never reads anything in the site's folders. Everything goes in its server block, in other words at vhost level.

The server vhost

The vhost (VirtualHost in Apache, server block in Nginx) is the site's configuration inside the web server itself. The same directives work there, but you need administrator access and a reload to apply them. This option does not exist on shared hosting, where one machine hosts many sites.

Which resources does each location use?

Contrary to a common belief, the three options are not equal in performance. What changes is the work done for each visit before the 301 status goes back to the user.

  • Vhost: the directives are read only once, at startup or reload, then kept in memory. No extra disk read, no PHP, no MySQL: the response goes out immediately.
  • .htaccess: Apache looks for and rereads these files in every directory up to the page, on every hit, and parses everything again. This costs disk reads and CPU time, all the more as the list grows. Still no PHP and no MySQL.
  • Plugin: each hit goes through the whole CMS. PHP starts (a PHP-FPM process and its memory), the CMS loads, MySQL is queried, then the answer goes out. It is the most expensive in CPU, RAM and database, and the slowest to the first byte.
Vhost.htaccess filePlugin
Configuration readOnce, at startupOn every hitOn every hit
Disk access per hitNoneOne per folderCMS code
PHPNoNoYes
MySQL queryNoNoYes
Response timeFastestFastSlowest

Hence the technical order of preference: vhost, then .htaccess, then plugin (vhost > .htaccess > plugin). With a few dozen URLs, the gap is small. With a migration of several thousand entries, and Google's crawlers revisiting every old URL, it becomes significant.

The same redirect, from .htaccess to vhost

Moving to the vhost needs almost no changes: the mod_rewrite directives are the same. Here is a specific example, an old HTML page sent to its new URL, then a whole old domain, in the three syntaxes.

# .htaccess file (Apache)
RewriteEngine On
RewriteRule ^old-page\.html$ https://www.new-domain.com/new-page/ [R=301,L]

# Apache vhost: same rule, path with a leading slash
<VirtualHost *:443>
    ServerName www.old-domain.com
    RewriteEngine On
    RewriteRule ^/old-page\.html$ https://www.new-domain.com/new-page/ [R=301,L]
    RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$ [NC]
    RewriteRule ^/(.*)$ https://www.new-domain.com/$1 [R=301,L]
    <Directory /var/www/site>
        AllowOverride None
    </Directory>
</VirtualHost>

# Nginx server block
server {
    server_name www.old-domain.com;
    location = /old-page.html { return 301 https://www.new-domain.com/new-page/; }
    location / { return 301 https://www.new-domain.com$request_uri; }
}

The AllowOverride None setting has one more benefit: Apache stops looking in the folders altogether, which removes that disk access. For SEO, the location changes nothing: Google sees the same 301 and passes the ranking signals, traffic and link equity of the old page to the new one in the same way. Only the speed differs, and with it the user experience.

Maintenance follows the reverse order

Ease of editing ranks the three options the other way round:

  • the plugin is edited from the back office, with no server access and no technical skills;
  • the .htaccess needs FTP or SSH access, but no restart;
  • the vhost needs administrator access and a reload, so often a system administrator.

Which location should you choose for your needs?

So choose according to the case, and nothing stops you from combining them:

  • Redesign, migration or domain change: the plan for all pages is set once, then frozen. Put it in the vhost, where it costs nothing per visit and will stay in place for years.
  • Day-to-day marketing fixes (a renamed page, a campaign, a discontinued product): handle them in the plugin, simpler for a non-technical team that does not have to call a developer for every new URL.
  • No access to the vhost (shared hosting): .htaccess is the right middle ground, much lighter than a plugin.

One last point, whatever you choose: the plan must be installed where the old domain points. If it is hosted on a different machine from the new site, that is where the configuration must go. Then test each old URL: it must return a 301 to the right page, without a 404 that would lose the links and rankings you have earned.

💡

How do you write the redirects?

Syntax of the Redirect, RedirectMatch and RewriteRule directives, redirecting a whole domain, HTTPS, home page, WordPress, cancelling a rule: find the full method in our guide .htaccess redirect: how to write a 301 or 302 redirect.

<p>Yes: Apache rereads it on every hit, while the vhost is loaded into memory once. The gap grows with the number of lines.</p>
<p>Not for a few redirects managed by marketing. For a migration plan with thousands of entries, prefer the vhost or .htaccess: the plugin loads PHP and MySQL on every visit.</p>
<p>In the <code>server</code> block of the site's configuration, with the <code>return 301</code> or <code>rewrite</code> directives. Nginx has no .htaccess file.</p>

Swan generates your redirect plan for the vhost or .htaccess, and checks every URL.

The SEO migration checklist, as a PDF. 20 pages of checks to tick off, from the decision to migrate to monitoring after go-live.

Read next

All posts