Permanent Redirects between two Domains

Okay, so I have two domains, let’s call them x.com and y.com.

To start off, x.com existed and was used for a personal website for many years. Then I moved all of the content from x.com to archive.x.com and started planning the new-birth of a new project. I launched the new project and eventually bought y.com and associated it to the new project.

So to summarize,
x.com was used for a personal website for many years
x.com was moved to archive.x.com
x.com was then briefly used for the launch of a new project
y.com was purchased for the primary use of the new project

Okay, now that we are all confused :D, I’ve noticed that search engine results are trying to get to pages that x.com once originally contained (yes, shame on me for not already properly redirecting these requests! – to be fair, I didn’t really care because none of these projects have money making potential at this point). But many of the searches I have seen are people looking for solutions that I documented on x.com at one point, so I feel bad by not redirecting them to the appropriate new location (I do have a conscious after all).

So now I am cleaning up my mess, and I think I did it correctly, but I would love some feedback.
First thing you should know x.com is a wordpress installation (and remains so on archive.x.com). y.com is also a wordpress installation (however, both installations use different permalink structures, so they are distinguishable – YAY!).

The first thing I did was update my .htaccess on y.com/x.com (both parked at the same location) to redirect x.com to archive.x.com

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteRule (.*)$ http://archive.x.org/$1 [R=301,L]

Then I noticed that a few pages that were indexed when x.com was pointing to the new project were going to archive.x.com creating a 404. So I added this to archive.x.com’s .htaccess

RewriteRule ^([-a-zA-Z0-9\\s]+/[-a-zA-Z0-9\\s]+/[0-9]+) http://y.com/$1 [R=301,L]
RewriteRule ^([0-9]+/[0-9]+/[-a-zA-Z0-9\\s]+/) http://y.com/$1 [R=301,L]

Now the only downside to this whole thing, is if a request comes pointing to a page part of the new project, but is using x.com instead of y.com, they will be redirected to archive.x.com which will in turn redirect them back to y.com (did you follow that?)

I believe I could resolve that issue by adding the same re-write rules I have on archive.x.com to x.com/y.com’s .htaccess (again since they are parked at the same location)
I’m thinking something like the below would be the final .htaccess changes for x.com/y.com

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteRule ^([-a-zA-Z0-9\\s]+/[-a-zA-Z0-9\\s]+/[0-9]+) http://y.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteRule ^([0-9]+/[0-9]+/[-a-zA-Z0-9\\s]+/) http://y.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteRule (.*)$ http://archive.x.org/$1 [R=301,L]

Does this look right to everyone else? Or did I create a fundamental blunder that I should be chastised for (yes, I talking to you @dklynn ; :p)?

Hey cp!

Thanks! I got a good chuckle out of your last question!

Okay, as I was reading, my first thought was ‘why not just exclude the “new stuff” from the redirection to the archive?’ Fortunately for you ( :whip: ), your code CORRECTLY excluded the “new stuff” by redirecting it to y.com first! :tup:

A word of warning before getting into your code (without looking forward at the details), you need to place your redirections/exclusions before WP’s “redirect everything” code.

Then, WHY the \s in your character range definition? ANY space (or tab, etc) character would be encoded in the URI so this is rather nonsensical (to me). Remember, if WP is smart enough to handle %20’s in your URI, they’ll be encoded in your character range definition as \{space} (I can’t simply write \ and make it understood that there is an extra space after the \).

Unfortunately, I can see no other problems (except that you’ve not retained/displayed the !-f and !-d which is typical of WP) and the use of \s in the character range definitions. Of course, I’d combine a few lines, too, but that’s simply technique.

In other words, being beaten in the past ( :rofl: ) has paid handsome dividends (assuming your scars have healed). You may not be a pro yet, but you’re getting really close. Kudos to you!

Regards,

DK

Yeah, this came up as a thought to me too as I wrote my original post, then I extended my post to include that concept :smiley:

Yep, got that covered too :slight_smile:

When you wrote that, that makes me wonder too, so I’ll have to look into that. I agree, the \s is likely unnecessary. Shouldn’t take much to test either.

I left that out intentionally, as I wanted it to go to its proper domain, as the !-f and !-d would still permit the incorrect domain from being used. Once it redirects, the appropriate domain’s .htaccess will apply !-f and !-d accordingly (I think).

Scars have definitely healed, and I find myself constantly asking myself “are you sure you need that set of characters, did you include too much and not exclude enough?” your expertise has been very helpful.

cp,

Well, you received the earned kudos for your exclusion, placement of your code (although I’ll reserve judgement on the lack of !-f and !-d as I think those are necessary for WP to exclude necessary support files), I believe I abused you appropriately for the \s in the character range definition (albeit, your remaining code is excellent) and I’m glad that I’ve made you paranoid about the content of the regex (specifically the character range definitions). It’s not fun to be paranoid but there is someone out there who’s looking to “get you … just because they can” (without even knowing it’s you they’re hacking).

Any more problems, just let me know (now that your scars have healed :rofl: )!

Regards,

DK

In regards to the !-f and !-d, if I did this

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-a-zA-Z0-9\\s]+/[-a-zA-Z0-9\\s]+/[0-9]+) http://y.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+/[0-9]+/[-a-zA-Z0-9\\s]+/) http://y.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^x.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.x.org$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ http://archive.x.org/$1 [R=301,L]

A request to x.org/myfolder/ would continue to use x.org instead of using archive.x.org/myfolder/ because the folder really does exist, which is not what I want. I really think I need to redirect first, then let the underlying archive.x.org htaccess do the !-f and !-d before processing the request. Does that make sense? Or am I really missing something here?

cp,

I think that I would have omitted the tests in the first two blocks but, in thinking about that, you WANT all the x requests sent to y. x would contain both this .htaccess and WP’s so I don’t think you’re in any trouble eliminating them.

The third block may or may not require these tests as it’s not supposed to support any requests.

Regards,

DK