# .htaccess to nginx / Caddy & nginx Linter API

> Convert Apache .htaccess to an nginx server block and a Caddyfile, lint nginx.conf for pitfalls, or generate WordPress, Laravel, SPA, proxy configs.

- Endpoint: `GET | POST https://apibox.io.vn/v1/webserver-config`
- Category: Developer utilities
- HTML docs: https://apibox.io.vn/docs/webserver-config/ (Vietnamese: https://apibox.io.vn/vi/docs/webserver-config/)
- Online tool: https://apibox.io.vn/tools/webserver-config/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

`convert` (default) turns an `.htaccess` into an nginx `server` block and a Caddyfile: mod_rewrite (RewriteCond / RewriteRule with flags L, R=301, NC, QSA, QSD, F, G, OR, RewriteBase, %N back-references), the front-controller pattern (→ `try_files`), HTTPS and www redirects, Redirect / RedirectMatch, ErrorDocument, DirectoryIndex, Options, Header, mod_expires (→ a `map` on the content type), mod_deflate, Deny / Allow / Require, <Files> / <FilesMatch>, basic auth, LimitRequestBody, ServerSignature. Each directive gets a `confidence` (high / medium / low / none) and a note; PHP settings (`php_value`, `php_flag`) are moved to a PHP-FPM pool snippet. The output passes `nginx -t` for the supported directives.

`lint` checks an nginx configuration statically: unbalanced braces and missing semicolons, `if` inside location ("if is evil"), alias path traversal (location without / + alias with /), add_header in a child block dropping the parent headers, proxy_pass with a URI in regex locations, variables without resolver, old TLS protocols and weak ciphers, `listen 443` without ssl, deprecated `listen … http2`, CRLF injection through `$uri` in redirects, unanchored / unsafe PHP locations, autoindex, duplicate server names, missing HSTS / security headers and `server_tokens`.

`generate` writes nginx and Caddy configs for `static`, `spa`, `wordpress`, `laravel`, `php` or `reverse-proxy` sites with HTTP → HTTPS and www / apex redirects, Let’s Encrypt paths, gzip (brotli optional), static-file caching and security headers. Test the result with `nginx -t` or `caddy validate` before going live.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `action` | body|query | string | no | `convert` | `convert`, `lint` or `generate` (alias `mode`; also inferred from the fields sent). |
| `htaccess` | body | string | no |  | convert: .htaccess content (max 300 KB). |
| `nginx` | body | string | no |  | lint: nginx configuration text (max 1 MB). |
| `server_name` | body|query | string | no | `example.com` | convert: server name for the generated block. |
| `root` | body|query | string | no |  | Document root (convert default /var/www/html, generate /var/www/<domain>). |
| `php_fpm` | body|query | string | no | `unix:/run/php/php8.3-fpm.sock` | PHP-FPM address. |
| `preset` | body|query | string | no |  | generate: `static`, `spa`, `wordpress`, `laravel`, `php`, `reverse-proxy`. |
| `domain` | body|query | string | no |  | generate: domain, e.g. `example.vn`. |
| `www` | body|query | string | no | `apex` | generate: `apex` (www → apex), `www` (apex → www) or `none`. |
| `upstream` | body|query | string | no | `http://127.0.0.1:3000` | generate reverse-proxy: backend URL. |
| `https` | body|query | boolean | no | `true` | generate: HTTPS with redirect. |
| `brotli` | body|query | boolean | no | `false` | generate: brotli (needs ngx_brotli). |
| `server` | body|query | string | no | `both` | generate: `nginx`, `caddy` or `both`. |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/webserver-config" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"htaccess":"RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\nRewriteRule ^old-page$ /new-page [R=301,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\nphp_value upload_max_filesize 64M\n","server_name":"example.com","root":"/var/www/example"}'
```

## Example response

```json
{
  "action": "convert",
  "nginx": "server {\n    listen 80;\n    listen [::]:80;\n    server_name _;\n    return 301 https://$host$request_uri;\n}\n\nserver {\n    listen 443 ssl;\n    listen [::]:443 ssl;\n    http2 on;\n    # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;\n    # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;\n    server_name example.com;\n    root /var/www/example;\n    index index.php index.html;\n\n    rewrite ^/old-page$ /new-page permanent;\n    location / {\n        try_files $uri $uri/ /index.php?$args;\n    }\n\n    location ~ \\.php$ {\n        try_files $uri =404;\n        fastcgi_split_path_info ^(.+\\.php)(/.+)$;\n        fastcgi_pass unix:/run/php/php8.3-fpm.sock;\n        fastcgi_index index.php;\n        include fastcgi_params;\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    }\n\n    location ~ /\\.(?!well-known) {\n        deny all;\n    }\n}\n",
  "caddy": "example.com {\n    root * /var/www/example\n    @r1 {\n        path_regexp r1 ^/old-page$\n    }\n    redir @r1 /new-page 301\n    try_files {path} {path}/ /index.php?{query}\n    php_fastcgi unix//run/php/php8.3-fpm.sock\n    file_server\n}\n",
  "php_fpm_pool": "php_value[upload_max_filesize] = 64M",
  "items": [
    {
      "line": 2,
      "directive": "RewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]",
      "nginx": "server { listen 80; return 301 https://$host$request_uri; }",
      "caddy": "(automatic: Caddy redirects HTTP to HTTPS by itself)",
      "confidence": "high",
      "note": "HTTPS redirect moved to a separate port-80 server block"
    },
    {
      "line": 5,
      "directive": "RewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]",
      "nginx": "location / { try_files $uri $uri/ /index.php?$args; }",
      "caddy": "try_files {path} {path}/ /index.php?{query}",
      "confidence": "high",
      "note": "front controller (WordPress / Laravel / most PHP apps): use try_files instead of if (!-f)"
    },
    "…"
  ],
  "confidence": {
    "high": 4,
    "none": 1
  },
  "parse_errors": [],
  "note": "Test the result (nginx -t, caddy validate) before going live: complex mod_rewrite rules are marked low confidence."
}
```

## Use cases

- Move a WordPress / Laravel site from cPanel (Apache) to an nginx VPS
- Convert legacy rewrite rules when switching to Caddy
- Review nginx configs for alias traversal and lost security headers
- Generate a hardened nginx or Caddy config for a new domain

## FAQ

### Is every mod_rewrite rule converted?

Common patterns are converted with high confidence; complex chains (several conditions, [P], [S], [C]) get low confidence and a note. Always run nginx -t and test the important URLs.

### Where do php_value lines go?

Not into nginx: the response gives php_value / php_flag lines for the PHP-FPM pool (or put them in php.ini / .user.ini).

### Does lint follow include files?

No: send the full configuration text. It is a static check, not nginx -t.
