How To Excluded a File From a Site Wide htaccess 301 Redirect

Gordon McNevin
29-12-2018 09:00:00

Recently Google asked me to verify a website that was 301 redirecting to another site because the site moved. I put this in the .htaccess so everything on the old domain would redirect to everything on the new domain but using the same url structure.

I thought that's all I needed, until I found the old domain was still in the Google index.

Add this point I headed over to the Google Webmaster Tools (Google Search Console), selected the old site still in the Google index, clicked the Gear/Settings Icon at the top right and selected the 'Change of Address' option to let Google know my website has moved in another way.

I then asked me to verify ownership of my old domain again with a verification file. I downloaded the file, uploaded to the root (olddomain.co.uk/google5b7b2a7491e6a30e.html) and tried to verify my site.

This is when Google couldn't see my verification file as the 301 was redirecting everything to my new domain.

After a little thinking the quickest way was to add an exclusion to the .htaccess file so I can exclude the Google verification file from the redirect. Now the .htaccess file with a site wide 301 redirect with a single exclusion looks like this.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/google5b7b2a7491e6a30e\.html$
RewriteRule ^(.*)$ https://www.newdomain.co.uk/$1 [R=301,L]

Now the Google verification file is loaded and approved successfully by Google and everything else 301 redirects to the new domain while keeping the same folder and directory structure as before.

Hope this helps!