Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk9 min

How to Fix .htaccess Rewrites Returning 404 on NGINX

NGINX never reads Apache .htaccess files. Translate the rules into the correct server and location blocks, use try_files for front-controller routing, and diagnose 404s by checking location precedence, root or alias paths, fallback handlers and logs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NGINX does not read Apache .htaccess files. If a site moved from Apache to NGINX, the old RewriteRule and RewriteCond directives are inert until you translate their behavior into the active NGINX server and location configuration. Without that translation, NGINX normally checks the configured document root for a literal file or directory, fails to find a pretty URL such as /products/42, and returns 404.

The usual front-controller fix is try_files $uri $uri/ /index.php?$args;, but it is not a universal copy-and-paste rule. The correct fallback depends on whether the site is static, uses PHP-FPM, or proxies to another application. The active root or alias, location precedence, and handler for the fallback URI must all agree.

As an Amazon Associate I earn from qualifying purchases.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the 404 actually means

Apache evaluates .htaccess in directory context for each request. NGINX has no equivalent per-directory file that it discovers automatically. Merely leaving .htaccess in the web root does nothing on NGINX; directives such as RewriteEngine On, RewriteCond, and RewriteRule are never parsed.

Suppose a browser requests /pretty-path. NGINX selects a server block, chooses the most specific matching location, and maps the URI to the active filesystem root or alias. If no real file or directory exists and no translated fallback sends the request to the application, NGINX ends the request with 404. A working homepage does not disprove a routing problem: / may resolve to an existing index.html or index.php while every extensionless route misses the filesystem.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

The repair therefore belongs in NGINX configuration, not in application code first. Translate the intent of the Apache rules, put it in the virtual host that serves the failing hostname, validate the configuration, reload NGINX, and then inspect logs if the result is still wrong.

Translate the intent, not the Apache syntax

Start by classifying what each Apache rule was trying to do. Apache configurations often mix several jobs that should become different NGINX directives.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Original intent NGINX tool What happens to the client Typical failure to watch
Send a permanent or temporary redirect return External redirect; the browser receives a 3xx response and requests a new URL Redirecting a path that should have been handled internally
Transform a URI with a regular expression rewrite Usually an internal URI change unless a redirect flag is used Rewrite loops, wrong replacement, or lost query arguments
Serve an existing file or directory, otherwise route elsewhere try_files Internal fallback; the original URL can remain in the address bar Wrong root/alias or a fallback with no handler
Pass a request to an application server Named location, proxy_pass, or a PHP-FPM location NGINX forwards the request internally or upstream Upstream receives the wrong URI or is not configured for the fallback

This separation avoids the common migration mistake of translating every Apache condition into an NGINX if. The NGINX conversion guidance favors server, location, and try_files for routing and existence checks. Use a conditional only when the intent genuinely requires one and no clearer location or file test expresses it.

The standard front-controller translation

For an application whose single entry point is /index.php, a common server shape is:

server {
    root /var/www/example/public;

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

    location ~ .php$ {
        # Use the PHP-FPM settings appropriate to this deployment.
    }
}

try_files evaluates candidates from left to right using the root or alias in the current context:

  • $uri checks whether the requested path maps to a real file.
  • $uri/ checks whether it maps to a real directory.
  • /index.php?$args is the final internal URI when neither candidate exists; $args preserves the original query string.

This is the NGINX equivalent of the familiar Apache logic “if the request is not a file and not a directory, rewrite it to the front controller.” It only works when the configured root really contains the public files, index.php exists at that path, and a matching PHP-FPM location can execute it. If your entry point is elsewhere, replace the final URI with that actual endpoint.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter | USB C Dongle with 90W Max Power Delivery, 3×5Gbps USB A Data Ports, for MacBook, Dell, and More (Charger Not Included)
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Static sites

A static site should not send unknown paths to PHP. If the desired behavior is an explicit 404, use:

location / {
    try_files $uri $uri/ =404;
}

If the site uses a generated single-page application, its fallback may be an HTML file instead:

location / {
    try_files $uri $uri/ /index.html;
}

That fallback is an internal route to the static shell; it does not make an API or server-rendered route exist.

PHP applications

Keep the filesystem check in the general location / block and ensure the fallback URI reaches the PHP handler. A rewrite that changes the URI successfully can still end in 404 if the PHP location is absent, excludes the new path, or points FastCGI at a nonexistent script. The exact FastCGI parameters vary by distribution and deployment, so copy the working PHP-FPM settings for your installation rather than inventing values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reverse-proxied applications

For an application listening behind NGINX, the final candidate can be a named location that proxies to the upstream:

location / {
    try_files $uri $uri/ @application;
}

location @application {
    proxy_pass http://app_backend;
}

Whether the upstream receives the original URI or a changed one depends on the proxy configuration. Confirm the application’s routing contract before adding a rewrite.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Check root, alias, and location precedence

try_files cannot find a file that NGINX is looking for in the wrong directory. A deployment often changes the public directory from a project root to something like /public; retaining the old root makes every candidate miss even though the files are present elsewhere.

root versus alias

With root, NGINX appends the request URI to the configured directory. With alias, the matching location is replaced by the alias path. That difference matters especially in prefix locations and can produce surprising file paths. Verify the directive that applies to the failing request, not merely a root value elsewhere in the file.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More-specific locations can bypass your fix

NGINX selects locations by its matching rules. A regular-expression location for assets, an exact location, or a nested block may handle the pretty URL before it reaches the location / containing try_files. Inspect all locations for the affected host and URI. A correct rule in an unused virtual host is indistinguishable from a missing rule from the browser’s perspective.

When to use return, rewrite, or try_files

Use return for a fixed redirect or status

For a moved resource with a known destination, a direct return is clearer and safer than a regular expression:

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

The client receives the redirect and makes a second request. Use the appropriate status for your migration policy; do not use a redirect when the application should handle the original URL internally.

Use rewrite for regex URI transformations

rewrite is appropriate when a pattern must be transformed, for example mapping a legacy path shape to a new internal path. Rules execute in order, and an internal rewrite can cause NGINX to search for another matching location. Repeated URI-rewrite processing is limited to ten cycles, so conflicting rules can terminate with an error instead of resolving.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray | Data transfer & Fast charging for MacBook, iPhone 17/16/15, iPad, Samsung Galaxy, Apple Watch
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
location /legacy/ {
    rewrite ^/legacy/(.*)$ /archive/$1 last;
}

Choose the flag deliberately: a redirect flag changes what the client sees, while an internal-processing flag keeps the original URL. Test whether the replacement should retain the existing query string; replacement syntax controls that behavior, and an intentional trailing question mark can discard existing arguments.

Use try_files for existence checks and fallback routing

try_files answers “does this file or directory exist under the active filesystem mapping, and if not, where should the request go?” It checks candidates in order and internally redirects to the last URI, named location, or configured status. It is usually the safest replacement for Apache conditions that test whether the requested filename is not a real file or directory.

Step-by-step 404 diagnosis

  1. Identify the running NGINX instance. Run nginx -v for the version and nginx -V for build details, including the compiled configuration path. On packaged systems, the service manager may point to a different binary or instance than the one you edited.
  2. Find the loaded configuration and logs. Use the configuration path reported by the installation and inspect the access and error log locations configured for the server. The request must be reaching the instance whose files you are changing.
  3. Validate before reloading. Run the deployment’s configuration test command, commonly nginx -t. Fix every syntax or include error it reports before attempting a reload. Then reload through the service manager so workers receive the new configuration without an unnecessary stop/start.
  4. Confirm the selected virtual host. Check that the request’s hostname matches the intended server_name. A default server or another certificate/host block can return a 404 while your edited block remains unused.
  5. Trace location matching. For the exact failing URI, determine whether an exact, prefix, or regular-expression location wins. Look for an asset or security location that bypasses the general fallback.
  6. Verify the filesystem mapping. Check the active root or alias and confirm that a known static file exists where NGINX expects it. A path typo, changed release directory, or incorrect alias causes every try_files candidate to fail.
  7. Test the fallback endpoint directly. Confirm that /index.php, a named proxy location, or the selected static fallback actually exists and has a handler. If the fallback itself returns 404, the rewrite is not the remaining problem; fix the handler or application route.
  8. Compare two requests in the logs. Request one known static file and one pretty or dynamic URL. Compare status codes, selected host, and error-log messages. This distinguishes a location/root issue from an upstream application response.
  9. Look for rewrite cycles. If a request changes URI repeatedly, inspect each rewrite and the locations it re-enters. Remove overlapping transformations and ensure the final URI no longer matches a rule that sends it back to the previous URI.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common migration mistakes and precise fixes

Leaving Apache directives in place

RewriteEngine On, RewriteCond, and RewriteRule have no effect in a file NGINX never reads. Move the behavior into an included NGINX configuration file inside the relevant server or location block, then validate and reload.

Replacing every condition with if

Many “not a file” and “not a directory” checks are one try_files statement. A location split or a named fallback is often clearer than nested conditionals. Keep conditions for cases that cannot be expressed by routing, and test their interaction with internal redirects.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using the wrong document root

Applications commonly expose only a public or web directory. Pointing NGINX at the repository root can expose unintended files while still making the expected front controller unavailable. Set the root to the directory that is meant to be web-accessible and verify the fallback there.

Omitting the application handler

A successful internal rewrite is not the same as a successful response. If the fallback URI has no PHP-FPM, proxy, or static handler, NGINX may return 404 or an upstream error. Trace the fallback through the next matching location.

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Changing query-string behavior accidentally

Legacy rules often depend on parameters such as ?id=42. In a front-controller pattern, append ?$args when preserving the original arguments is required. For a regular-expression rewrite, verify whether the replacement keeps, replaces, or intentionally removes the existing query string.

A safe validation checklist

  • The request hostname selects the intended server block.
  • The winning location contains the routing rule you changed.
  • The active root or alias maps to the real public directory.
  • A known static file succeeds through the same location hierarchy.
  • The final try_files target exists and has a matching handler.
  • Query arguments survive the internal route when the application needs them.
  • nginx -t passes before reload, and the reload completes successfully.
  • Access and error logs show one expected route rather than a rewrite loop.
  • Static, dynamic, missing, and legacy URLs are tested separately.

What a correct result looks like

A real file such as /assets/app.css is served directly. A real directory is handled according to its index or directory policy. A pretty application URL such as /account/settings remains visible in the browser while NGINX internally sends it to the configured front controller or upstream. A genuinely unknown static path returns the explicit status you selected, commonly 404. A legacy URL that was intentionally moved returns a redirect and lands on its new destination.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If those outcomes do not match, return to the selected host, winning location, filesystem mapping, and fallback handler in that order. Those four pieces account for most “.htaccess works on Apache but every pretty URL is 404 on NGINX” migrations.

Frequently Asked Questions

Can I make NGINX read the existing .htaccess file?

No. NGINX does not parse Apache per-directory configuration files. The routing behavior must be represented in NGINX configuration.

Why does / work while /about or /products/42 returns 404?

The homepage is often an actual index file, while extensionless paths require a fallback such as try_files to reach the application. Check the winning location and final fallback URI.

What should the last try_files argument be?

Use the real endpoint for the application: an existing index file, a named proxy location, an HTML shell, or an explicit status such as =404. It must match the handlers configured on that server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Could the application itself be generating the 404?

Yes. If NGINX reaches the fallback and the upstream or application returns 404, the rewrite is functioning but the application route, script path, or proxy URI needs correction.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.