Migrating a site from Apache to Nginx means translating mod_rewrite rules, which live in .htaccess, into Nginx’s rewrite, return, and location directives. The two engines share a PCRE-style regex syntax but differ in structure and flag handling. This .htaccess to Nginx converter parses your rewrite rules and produces matching Nginx blocks in your browser.
How it works
Apache rewriting is organised into rule blocks: zero or more RewriteCond lines followed by a single RewriteRule they apply to. The converter walks your input and processes each block:
- A
RewriteRulewithR=301orR=302becomes areturn <code> <target>;. - A plain internal rewrite becomes
rewrite "<pattern>" <target> last;orbreak. Fbecomesreturn 403;andGbecomesreturn 410;.- A preceding
RewriteCondtesting%{HTTP_HOST},%{REQUEST_URI},%{QUERY_STRING}, or%{HTTPS}becomes anifguard wrapping the rule.
Because Apache per-directory rules match the path without a leading slash but Nginx matches with one, the converter adds a leading slash to anchored patterns and notes when it does so.
Example
The Apache rule:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
converts to an Nginx host guard with a permanent redirect:
if ($host ~* "^example\.com$") {
return 301 https://www.example.com/$1;
}
Notes
This covers the most common directives, not every edge of mod_rewrite. Nginx does not chain if conditions the way Apache chains RewriteCond, and file-existence checks (-f, -d) map to try_files rather than an if. Read the conversion notes, then test each block against real URLs before deploying. Everything runs locally, so your rules stay private.