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.
Table of Contents
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.htmlwith the filename you want to exclude.target-domain.comwith 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.