Tutorial · migration-hosting · Published 2026-08-16 · 4 min read

Moving a website from Apache to nginx

Apache to nginx migration: port .htaccess rules, wire PHP-FPM, map modules and extensions, and verify URLs, redirects and rewrites after the switch.

Flat editorial illustration showing two data boxes crossing a bridge with a dotted orbit trail between them, one box accepting the transfer.
Illustration: this article at a glance.

The migration is config, not content

Moving a site from Apache to nginx (a common host change where the panel or a new server runs nginx, or a micro-optimisation to gain the file-serving edge) does not touch your HTML, files, or database. It transplants the server configuration. The rules that Apache lived in .htaccess and httpd.conf must become nginx location and rewrite blocks in nginx.conf, and PHP handling changes from Apache's module to a PHP-FPM pool.

Editorial close-up illustration showing two data boxes crossing a bridge with a dotted orbit trail between them, one box accepting the transfer.
Illustration: a closer look at the technique described above.

Where the two differ for a migration:

ConcernApachenginx
Config location.htaccess per directoryOne server block, per-site .conf file
RewritesRewriteRule in .htaccessrewrite, location blocks, try_files
PHPmod_php or a FastCGI handlerPHP-FPM via fastcgi_pass
Directory accessPer-directory Directory sectionslocation / and try_files

The host migration checklist is the transport side (files, DB, DNS); this article is the *config side* of the same move.

Step 1: inventory .htaccess

Every line in .htaccess (and any Directory/VirtualHost block in the Apache config) needs a translation. Read it first and sort the rules by pattern:

RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ product.php?id=$1 [L]

in nginx:

location /products {
    rewrite ^/products/([0-9]+)/?$ /product.php?id=$1 last;
}

The mapping is mechanical for simple rules but the edge cases bite: a rule that uses [R=301] (a permanent redirect) must keep its full URL and permanent, while a rule that only changes the internal path uses last and no R. Mixing the two is how an "Apache to nginx" migration silently turns every old 301 into a broken redirect.

Step 2: map the rewrite flags

ApachenginxMeaning
[R=301]redirect permanentClient-visible 301
[L]lastStop processing this rule set
[NC](?i) prefix in the patternCase-insensitive
.htaccess honoured per-dirlocation blocksScope the rules to a path

Ignore rules you do not need: a .htaccess often contains old Options and AddType lines from a panel that nginx does not use at all.

Step 3: wire PHP and FastCGI

nginx does not run PHP through a module; it describes a location that hands .php requests to a PHP-FPM listener:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Notes:

Step 4: The WordPress-specific part

WordPress ships a .htaccess for pretty permalinks. In nginx, that rest is try_files:

location / {
    try_files $uri $uri/ /index.php?$args;
}

Every CMS has this one line, and it is the difference between the homepage working and every page 404. The redirects article has the general 301/redirect semantics that apply if you move URL structure at the same time.

Step 5: Verify like a build gate

Before the DNS cutover (or in a temporary domain), test on the new server:

  1. The homepage and a deep path (the rewrite above should serve both).
  2. A 301 rule: curl -I https://new.example.com/old-page returns 301 and the right Location.
  3. PHP: curl -I https://new.example.com/index.php returns HTTP 200, not text or a 404.
  4. The exact paths from .htaccess that redirect: run the same list against both servers and diff the status codes.

Use the hosts-file trick from the host migration checklist so the new stack answers your domain before the switch.

Prevention

The neighbouring articles cover the transport (moving hosts and DNS) and the redirect semantics that make the URL rules correct once the server software changes.

Need a website built, fixed, optimised, migrated or replaced?

This technical resource is written by CSMBAC, a small design and development studio. If you would rather hand the problem to a professional, the website service page explains how we build enquiry-ready websites.

Explore website services