Creating a clean URL with an .htaccess rewrite

A rewrite lets a visitor use a short address while the server loads a different file internally. Unlike an external redirect, it does not normally change the address in the browser. Use it for a specific routing requirement, and preserve the rules generated by your CMS.

These instructions apply where the web server supports Apache-compatible .htaccess rules. Native NGINX does not read this file. A CDN or reverse proxy can also affect the request seen by the origin server. Keep File Manager or SFTP available so you can restore the previous file if a rule prevents browser access.

01 Create a specific rewrite

  1. Download a copy of the existing .htaccess from the website's document root. In File Manager, enable display of hidden files if necessary.
  2. Confirm that the target file exists. In this example, /sitemap will load /files/folder/sitemap.html. These are illustrative paths; change them for your site.
  3. Place the specific rule before a CMS catch-all block, outside any section that the CMS says it manages. Do not replace the whole file.
Code or configuration example
RewriteEngine On
RewriteRule ^sitemap/?$ files/folder/sitemap.html [END]

This Apache 2.4 example matches only the requested path with an optional trailing slash. In a document-root .htaccess, the pattern does not start with a slash. If your compatible web server supports only the usual [L] flag, confirm its behavior with support before adapting the example.

02 Check routing and public links

  1. Open https://example.com/sitemap and verify the expected content while the address remains unchanged.
  2. Test unrelated pages and an address that should return 404. A broad rule should not accidentally turn missing pages into a successful home page response.
  3. If both the short and original file URLs remain public, choose a canonical URL in your application or SEO configuration. Use a deliberate redirect only when you intend the browser to move.

How do I recover from a rewrite error or redirect loop?

Restore the saved file if you receive an immediate 500 response, then inspect the error log. Check rewrite support, spelling, the rule order and the real target path. Redirect loops often involve a CMS rule or HTTPS/canonical-host redirect in another layer. Test one change at a time and clear only the relevant cache after fixing the cause.

  • 0 Users Found This Useful
  • development
Was this answer helpful?

Related Articles

Redirecting a page with .htaccess and checking the result

An HTTP redirect tells browsers and search engines that a resource has another address. Use a...