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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use one public, canonical URL such as /about, while keeping about.html on the server. Permanently redirect requests for the old .html address to the clean URL, then internally rewrite the clean URL back to the file. Removing a directory works the same way, but it is a separate URL migration: map every old path explicitly, check for slug collisions, and update links, sitemaps, and canonical tags.

What “removing .html” and “removing a directory” actually mean

A browser requests a URL, not a filename. Your web server can therefore publish /about while reading /about.html from disk. Two different operations make that work:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • External redirect: a request for the legacy URL, such as /about.html, receives an HTTP 301 response to /about. The browser and search engines learn which address is canonical.
  • Internal rewrite: a request for /about is handled internally as about.html. The address bar does not change, and the visitor receives the file.

Do not let both forms return a successful 200 response. That creates duplicate URLs, splits links and analytics, and makes it unclear which address should be indexed. A directory removal is not achieved by the extension rule alone. For example, changing /docs/about.html to /about requires a dedicated redirect and a decision about what happens to any existing /about.html or /about resource.

Choose the canonical URL before changing server rules

Write down the final URL for every page before deployment. Decide whether the site uses a trailing slash, for example /about or /about/, and use that choice consistently. Then prepare a redirect table containing the old path, the new path, and the expected status code.

Old request Canonical result What the server should do
/about.html /about 301 redirect
/about /about Internal rewrite to about.html; return 200
/docs/about.html /about Explicit 301 redirect if this is the intended migration
/images/logo.svg /images/logo.svg Serve the existing file; do not send it through the page rule

Before selecting a destination, verify that /docs/about.html, /about.html, and /about do not represent three different resources. A collision can silently send visitors to the wrong page. Keep the redirect table under version control so that every renamed URL remains accounted for.

Apache: remove .html with .htaccess

Apache’s mod_rewrite reads regular-expression rules. In per-directory context, such as a document-root .htaccess file, Apache removes the directory prefix before matching a pattern. Consequently, patterns must be relative to the directory containing the file: use ^about.html$ rather than a pattern beginning with the full filesystem path.

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

The following document-root pattern first redirects a directly requested legacy filename, then serves an extensionless request from its corresponding HTML file:

RewriteEngine On

# Redirect the legacy extension to the canonical URL
RewriteCond %{THE_REQUEST} s/+(.+?).html(?:[?s]) [NC]
RewriteRule ^(.+).html$ /$1 [R=301,L,NE]

# Serve the real file without exposing the extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [END]

Why the Apache rules are ordered this way

  • RewriteEngine On enables rewriting for this directory.
  • %{THE_REQUEST} contains the original HTTP request line. Testing it prevents the internal rewrite to about.html from being redirected back out, which would otherwise create a loop.
  • [R=301,L,NE] sends a permanent redirect, stops processing this round, and avoids unnecessary escaping of characters already present in the target. A redirect substitution without a question mark keeps the incoming query string by default.
  • !-f and !-d exclude real files and directories. Without these conditions, a catch-all page rule can break images, stylesheets, JavaScript, downloads, and directory handling.
  • The file-existence condition ensures that only an extensionless path with a matching .html file is rewritten. Unknown paths continue to a normal 404 instead of being rewritten to a nonexistent file.
  • [END] terminates rewriting for the current request on Apache versions that support it. On older installations, use the appropriate [L] behavior and test for repeated per-directory passes.

The file lookup shown uses %{DOCUMENT_ROOT}. Adapt it when the site is in an alias, a subdirectory, a symlinked release directory, or another layout where that variable is not the physical document root. If overrides are disabled, place equivalent directives in the virtual-host configuration; a .htaccess file cannot override server settings that disallow it.

Apache directory migration

Extension removal and directory removal should be separate rules. To move /docs/about.html to /about, add an explicit mapping before the general extension rule:

RewriteRule ^docs/(.+).html$ /$1 [R=301,L]

This pattern is intentionally narrow. If only selected documents move, use one rule per path or a carefully maintained mapping rather than a broad rule that could flatten unrelated directories. Check the destination for collisions before enabling it. The old directory may also contain assets or subdirectories that must remain at their original locations.

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

Nginx: remove .html in the server block

Nginx does not read Apache-style .htaccess files. Put rewrite directives in the relevant server block, then validate the configuration and reload Nginx. For a static site, this pattern tests the requested path, its directory form, and its .html counterpart:

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

# Legacy extension redirect
location ~ ^/(.+).html$ {
    return 301 /$1;
}

How the Nginx configuration behaves

  • try_files checks whether each candidate file or directory exists. A request for /about can therefore be served from /about.html, while an unknown path returns 404.
  • The regular-expression location catches a request that visibly ends in .html and returns a permanent redirect to the extensionless path.
  • Nginx preserves the original query string when the return target has no new query string. Verify this with a URL such as /about.html?campaign=spring.
  • Location precedence matters. Keep the redirect location and any application-specific locations arranged so that the intended block handles the request.

For a directory move, use a dedicated permanent rewrite, for example:

rewrite ^/docs/(.+).html$ /$1 permanent;

Use this only when every matching old path has the same destination pattern. Otherwise, maintain explicit redirects. Nginx’s try_files is useful for static files; it is not a substitute for an application’s router.

Application sites and front controllers

Framework-based sites often route clean URLs through one front controller instead of mapping every page to a physical HTML file. In that case, do not blindly append .html to every missing request. Pass the request to the application’s documented entry point after allowing existing files and directories through.

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

A typical Nginx WordPress-style pattern is:

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

The application then decides whether /about is a page, a post, an API route, or a 404. If the site previously exposed .html URLs, create redirects at the web-server or application layer based on the old-to-new map, and update the CMS’s permalink and canonical settings. Do not combine a static-file rewrite and a front-controller fallback without testing which one wins.

Removing a directory segment safely

A directory segment can be a physical folder, a URL convention, or both. Removing it changes the path identity and therefore needs its own migration plan.

1. Inventory every old path

List HTML pages, feeds, downloadable files, images, language variants, and paths with query strings under the old directory. Decide which items move, which remain, and which should return 410 or 404. Do not assume that a page rule should also move assets.

2. Check slug collisions

Compare the proposed destination against all existing top-level slugs. A move from /docs/about.html to /about is unsafe if /about already serves a different resource. Resolve the conflict by choosing a different destination or retiring one resource deliberately.

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

3. Add the narrowest redirect

Place explicit old-path redirects before generic extensionless rules. Preserve path case behavior appropriate to the server and avoid accepting arbitrary host names or untrusted input in a redirect target.

4. Update references

Change navigation, breadcrumbs, structured-data URLs, canonical tags, XML sitemaps, feeds, documentation, and campaign links to the final form. Internal links should not rely on a redirect.

5. Monitor after launch

Review access logs, analytics landing pages, and search-console crawl errors for missed paths and redirect chains. Keep redirects for as long as old links can reasonably remain in circulation.

Deployment checklist

  1. Select one canonical extensionless form and one slash policy.
  2. Create an old-to-new redirect table, including directory moves and retired pages.
  3. Update internal links, XML sitemaps, canonical tags, feeds, and application settings.
  4. Put Apache rules in .htaccess only when overrides are enabled; otherwise use the virtual-host configuration.
  5. Put Nginx directives in the correct server block, not in a nonexistent .htaccess file.
  6. Deploy the 301 redirect before or alongside the internal rewrite so old links resolve immediately.
  7. Exclude real files and directories from catch-all rules.
  8. Validate the configuration before reloading the service. Keep a rollback copy of the previous configuration.
  9. Test extensionless, legacy, slash, no-slash, query-string, encoded-character, asset, directory, and unknown paths.
  10. Confirm that every intended final URL returns 200, every legacy URL returns one 301 to the final URL, and no request loops or chains through multiple redirects.

Testing commands and expected results

Use headers-only requests so you can inspect each hop without downloading the page:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -I https://example.com/about.html
curl -I https://example.com/about
curl -I "https://example.com/about.html?campaign=spring"
curl -I https://example.com/docs/about.html
curl -I https://example.com/images/logo.svg
curl -I https://example.com/does-not-exist

Replace example.com with your own host. The legacy request should show one 301 and a Location header pointing to the canonical path, retaining the query string. The clean request should finish with 200. The asset should still be served directly, and the unknown path should remain a genuine 404 unless your application intentionally supplies a not-found page.

Follow redirects when checking the complete chain:

curl -I -L https://example.com/about.html

A single 301 followed by a 200 is preferable to a chain such as old extension to old directory to clean URL. Test URL-encoded spaces, percent-encoded slashes, repeated slashes, and unusual characters permitted by your application. Redirect patterns that accept unvalidated user input can create unsafe redirects or expose unintended paths; constrain matches to your own host and the path characters your site actually allows.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common failure modes and fixes

The browser reports a redirect loop

The external rule is probably also matching the internally rewritten filename. On Apache, test THE_REQUEST so only a filename typed by the client is redirected. On Nginx, ensure the regex redirect location is not being re-entered after an internal fallback.

Every clean URL returns 404

Check the physical path used by the file-existence test, the document root, file permissions, and whether the rewrite is running in the expected directory context. In Nginx, confirm that try_files points to the correct root and that the configuration was reloaded.

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.

Images or scripts break

The catch-all rule is handling existing assets. Add or restore the checks that exclude real files and directories, and verify that relative asset URLs still resolve from the new page path.

Old URLs redirect, but parameters disappear

A redirect replacement containing a question mark replaces the query string. Remove that replacement unless you intentionally need to transform parameters, then retest campaign and tracking URLs.

Both slash forms work as 200 responses

Choose one slash policy and redirect the other form. Update internal links so visitors and crawlers encounter the canonical form directly rather than relying on a redirect.

One directory rule sends unrelated pages to the wrong place

The pattern is too broad or a destination slug collides with an existing resource. Replace it with explicit mappings or a narrower expression, and review the redirect table against the complete URL inventory.

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

Apache rules appear to do nothing

Confirm that mod_rewrite is loaded, that overrides are permitted for the directory, and that the file is named and located correctly. If the host disables overrides, move the directives to the virtual-host configuration.

Apache and Nginx compared

Concern Apache Nginx
Rule location .htaccess in per-directory context when overrides are enabled, or virtual-host configuration server block configuration; no .htaccess equivalent
Legacy redirect RewriteRule with R=301, commonly guarded by THE_REQUEST return 301 in a matching location, or rewrite ... permanent
Internal static lookup Rewrite to the matching file after excluding existing files and directories try_files $uri $uri/ $uri.html =404;
Application routing Pass unmatched requests to the application’s configured front controller Use the framework’s documented try_files fallback, such as an index.php entry point
Main safety check Keep patterns relative to the .htaccess directory and avoid unvalidated redirect input Check location precedence, destination collisions, and redirect loops

SEO details that prevent avoidable damage

  • Use HTTP 301 for a permanent migration, not a temporary 302.
  • Point canonical tags and XML sitemaps directly to the final extensionless URLs.
  • Replace internal links instead of leaving them pointed at redirecting addresses.
  • Redirect each old URL to its closest equivalent, not automatically to the home page.
  • Preserve query strings unless a documented parameter migration requires a change.
  • Watch for redirect chains, soft 404s, accidental noindex directives, and mixed slash policies after release.

The rewrite itself does not improve rankings; its purpose is to give users and crawlers one stable address while preserving existing links to the old one.

Frequently Asked Questions

Can I delete the .html files after adding the rewrite?

Not if the rewrite still maps clean URLs to those files. Keep the files, or replace the static-file mapping with an application route that serves equivalent content and retains redirects from the old addresses.

Will a 301 redirect preserve URL query parameters?

Normally, yes: a redirect target that does not specify a new query string keeps the original one. Verify this with your server and any proxy or application layer, especially when intentionally transforming parameters.

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

Do I need to remove the physical directory from the server?

No. A URL directory segment and a filesystem directory are separate concerns. You can leave the directory in place while redirecting its public URLs, provided static assets and other required paths continue to work.

Why should existing files and directories be excluded from the catch-all rule?

Without those exclusions, requests for assets or real directories can be rewritten as HTML pages, causing broken styles, scripts, downloads, directory behavior, or unexpected 404 responses.

How long should old .html redirects remain active?

Keep them while external links, bookmarks, feeds, and search indexes may still use the old paths. Review logs and crawl errors before considering removal, and retain redirects for important historical URLs.

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.

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.