How to Convert Apache .htaccess Rules to Nginx

How to Convert Apache .htaccess Rules to Nginx

If you are moving a site from Apache to Nginx, your .htaccess file stops working the moment you switch. Nginx does not read .htaccess at all. Every rewrite, redirect, and HTTPS rule has to be translated into Nginx server and location blocks instead. Here is how the two systems differ and how to map the rules you already have.

Why Apache to Nginx is not a copy-paste job

Apache reads a per-directory .htaccess file at request time, which lets you drop rules anywhere in your tree without touching the main config. Nginx has no equivalent. All configuration lives in the main config files (the server and location blocks), it is read once when Nginx starts, and changes only take effect after you reload.

That changes three things:

  • No per-directory config. You cannot scatter rules across folders. Everything goes in one place.
  • Different directives. Apache’s RewriteRule and RewriteCond from mod_rewrite become Nginx’s rewrite, return, and try_files.
  • Regex differences. Both use regex, but the syntax and capture group handling differ, so rules need adjusting rather than pasting.

Mapping common .htaccess rules to Nginx

These are the patterns you will hit most often.

A 301 redirect. In Apache:

RewriteRule ^old-page$ /new-page [R=301,L]

In Nginx, prefer a clean return over a rewrite:

location = /old-page {
    return 301 /new-page;
}

Force HTTPS. Apache uses a RewriteCond on %{HTTPS}. Nginx handles it with a dedicated port 80 server block:

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

The WordPress front controller. This is the rule most people are looking for. Apache’s default block routes missing files to index.php. The Nginx equivalent is one line inside your location / block:

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

try_files checks for the file, then the directory, then falls back to the front controller, which is exactly what the WordPress .htaccess rule does.

Convert your rules in four steps

Translating by hand is error-prone, so let the Apache to Nginx Config Converter do the parsing.

  1. Open the Apache to Nginx Config Converter and paste your full .htaccess contents.
  2. Read the generated Nginx block and confirm each rule mapped to a sensible rewrite, return, or try_files.
  3. Paste the result into your site’s server block inside the Nginx config.
  4. Reload Nginx with nginx -s reload (run nginx -t first to catch syntax errors).

The converter runs entirely in your browser, so your config never leaves your device.

Gotchas to watch for

  • Reload, do not just save. Nginx will keep serving the old config until you reload. If a rule “does not work,” that is usually why.
  • Test before reloading. nginx -t validates the config and prevents a broken reload from taking the site down.
  • Check your regex. Anchors and capture groups behave differently, so verify any complex rewrite by hand after converting.
  • Order matters. Nginx evaluates location blocks by a specific matching order, not top to bottom like Apache rules.

Paste your .htaccess, convert it, drop the result into your server block, and reload Nginx.

← All posts