How to Exclude a Specific File from a 301 Redirect

On this page
  1. Redirect Rule with File Exclusion
  2. How It Works
  3. Example
  4. Excluding Multiple Files
  5. Where to Place the Rule
  6. Summary

When configuring a 301 redirect in your .htaccess file, there may be situations where a specific file must remain accessible under the original domain.

A common example is a domain verification file used by services such as:

  • Google Search Console
  • Bing Webmaster Tools
  • Facebook Business Manager
  • Other website ownership verification services

In such cases, you can create an exception that excludes the selected file from the redirect rule.

Redirect Rule with File Exclusion

Add the following rules to your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/selected-file\.html$
RewriteRule ^(.*)$ https://target-domain.com/$1 [R=301,L]

How It Works

The rule:

RewriteCond %{REQUEST_URI} !^/selected-file\.html$

tells Apache not to apply the redirect when the requested URL matches the specified file.

All other requests will be redirected to the target domain using a permanent (301) redirect.

Example

Suppose:

  • Your current domain is:
old-domain.com
  • Your new domain is:
new-domain.com
  • You need to keep the verification file:
google1234567890abcdef.html

accessible on the original domain.

Use:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/google1234567890abcdef\.html$
RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

The result will be:

https://old-domain.com/google1234567890abcdef.html

remains accessible, while:

https://old-domain.com/about-us

redirects to:

https://new-domain.com/about-us

Excluding Multiple Files

If you need to exclude more than one file, you can use an additional condition:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/google1234567890abcdef\.html$
RewriteCond %{REQUEST_URI} !^/bing-site-verification\.xml$

RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L]

Both files will remain accessible on the original domain.

Where to Place the Rule

Add the redirect rule to the .htaccess file located in your website’s root directory.

By default, this is:

public_html

Summary

To exclude a specific file from a 301 redirect, add a RewriteCond rule before the redirect:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/selected-file\.html$
RewriteRule ^(.*)$ https://target-domain.com/$1 [R=301,L]

Replace:

  • selected-file.html with the filename you want to exclude.
  • target-domain.com with the destination domain for the redirect.

This allows verification files and other important resources to remain accessible while all other traffic is redirected to the new domain.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.