Jan 202010
 

.htaccess problems and can be a real pain, especially if like me you only do things with the .htaccess file every now and then so aren’t completely comfortable with the syntax.  Last night when helping a friend and author of the excellent ExchangeWire (about tracking the nascent ad exchange and ad trading networks) switch a domain, I configured the .htaccess file using a standard format:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www. oldomain.com$
RewriteRule ^(.*)$ “http\:\/\/www\.newdomain\.com\/$1″ [R=301,L]

I say “a” standard format because as anyone who has used .htaccess before knows, there are a few variations on this syntax that do the same thing.  Not thinking about it much I put this in the root .htaccess of the WordPress install, I tested the top domain, worked fine, but the pages themselves just kept redirecting to the the root, not the actual page.  My complete .htaccess file that was not redirecting properly:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www. oldomain.com$
RewriteRule ^(.*)$ “http\:\/\/www\.newdomain\.com\/$1″ [R=301,L]

Ok so the clued up amongst you will realise the problem was caused because of the ordering of the commands I had put in the file, this is important. In terms of importance, the domain redirection had to happen first, so I needed to put it above the WordPress command otherwise the WordPress command hijacked the process and confused the primary redirect.  So, the lesson is be careful of the order you put things in and the potential conflicts that can occur as a result.  Keep the major redirects/rules at the top of the page (e.g. when you want to change domains) and the more detailed one(s) below for more consistency (such as WordPress redirects).