How to redirect a URL with .htaccess and check the file before upload

How to redirect a URL with .htaccess and check the file before upload

A moved page needs one permanent redirect to its new address, and one typo in .htaccess can take the whole site down with a 500 error. Build the rules, validate the file, and keep a fallback for hosts without Apache.

redirect a url with .htaccess

One broken line, or one redirect too many

Apache reads .htaccess on every request to that folder. When it meets a directive it does not understand or that is not allowed there, it stops and returns a 500 Internal Server Error. A misspelled directive, a missing module, or a block that is never closed is enough. The server's error log names the exact line, but by then visitors are already seeing the error page.

A rule that works can still be slow. A redirect chain is several redirects in a row, such as http to https, then www to non-www, then the old page to the new one. Each hop adds delay, and search engines may stop following a long chain. The code matters too:

CodeMeaningUse it when
301Moved permanentlyA page or site has moved for good. Search engines move rankings to the new URL.
302Found (temporary)The move is temporary, such as a sale page or maintenance.
307Temporary redirectLike 302, but a POST stays a POST.
308Permanent redirectLike 301, but the method and body are kept.

Browsers cache a 301, so a wrong permanent redirect can keep firing on your own machine after you fix the file. Behind a CDN that ends HTTPS, such as Cloudflare in Flexible mode, a rule that checks %{HTTPS} always sees it as off and loops.

Generate the rules, validate the file, keep a fallback

Write the rules in the .htaccess Redirect Generator. Enter your domain and the redirect type, then tick the site-wide rules you need: Force HTTPS, www or non-www, Trailing slash, or Move to new domain. Add one row under Page redirects for each old URL. Every rule uses mod_rewrite and starts with RewriteEngine On. Page redirects come first. The old path becomes an anchored pattern with special characters escaped, and /? also catches the same path with a trailing slash. A relative target is turned into a full URL on your preferred host, so the visitor lands on the final address in one step. HTTPS and www are joined in one rule with [OR] conditions:

RewriteEngine On

# Page redirects
RewriteRule ^old-page\.html/?$ https://example.com/new-page [R=301,L]

# HTTPS and non-www in one redirect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

Paste the result near the top of the .htaccess file in the document root, above any block your CMS already has, such as the WordPress one.

Before you upload, paste the whole file into the .htaccess Validator. It reads the file line by line the way Apache does and lists errors and warnings with line numbers. It compares each directive with the ones allowed in .htaccess and suggests a fix for a close misspelling, such as RewriteCondtion. It checks that blocks like <IfModule> are closed in the right order, and it marks <Directory> and <VirtualHost> as errors because Apache refuses them there. It checks RewriteRule and RewriteCond flags, compiles each pattern to catch unbalanced brackets, and range-checks Redirect and ErrorDocument codes. The file is checked in your browser. The validator checks syntax only. It cannot test what a rule does to a real URL or know which modules your server loads.

Some hosts give you no server config at all, such as GitHub Pages or a plain file host. There, use the Meta Redirect Generator. Enter the target URL and a delay in seconds, and it builds a page with a meta refresh tag, a location.replace() fallback that keeps the old page out of the Back button history, a canonical link to the new URL, noindex, and a visible link. Only http and https targets are accepted. Google treats a 0-second meta refresh as a permanent redirect and a delayed one may be treated as temporary. A server 301 is still the better choice when you can set one.

Build, validate, and test a 301 redirect

  1. Enter your domain in the .htaccess Redirect Generator, choose 301, and add a row for each old path and its new address.
  2. Tick Force HTTPS and the www rule only if the site does not already do this somewhere else, so visitors get one hop instead of two.
  3. Paste the generated rules near the top of your .htaccess, above the CMS block, and run the whole file through the .htaccess Validator.
  4. Fix every error the validator lists, then upload the file to a staging copy and open each old URL. Clear the browser cache between tries.
  5. If the host ignores .htaccess, upload a page from the Meta Redirect Generator with a delay of 0 in place of the old page.

What these tools cannot check for you

  • The rules need Apache with mod_rewrite and AllowOverride enabled. Nginx, IIS, and most Node hosts ignore .htaccess.
  • Page redirects match the path only. A redirect based on the query string needs an extra RewriteCond %{QUERY_STRING} line.
  • The validator checks syntax with JavaScript regular expressions. Apache uses PCRE, so rare PCRE-only syntax may be flagged, and third-party directives show as unknown warnings.
  • A file that passes can still fail on a server whose AllowOverride forbids a directive. A meta refresh is a fallback, not an equal of a server 301.

Frequently asked questions

Why does my .htaccess file cause a 500 error?

Apache stops at any directive it does not understand or that is not allowed there. A typo, a missing module, or an unclosed block are the usual causes. The server error log names the line.

Should I use a 301 or a 302 redirect for a moved page?

Use 301 when the move is permanent. It tells search engines to index the new URL. Use 302 only for a temporary move.

Does .htaccess work on Nginx?

No. Nginx, IIS, and most Node hosts ignore .htaccess. The redirect has to go in that server's own config.

Why does my old redirect still happen after I changed the rule?

Browsers cache 301 redirects. Clear the browser cache or test in a private window after each change.

Tools used in this guide

  • .htaccess ValidatorDeveloper Tools

    Checks .htaccess files for unknown directives, unclosed blocks, and bad rewrite rules.

  • Meta Redirect GeneratorDeveloper Tools

    Builds a meta refresh redirect page with a JavaScript fallback and a visible link.

More from the blog

All guides