SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)

    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 , 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
    Code:
    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
    Code:
    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
    Code:
    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 ; )?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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 ( ), your code CORRECTLY excluded the "new stuff" by redirecting it to y.com first!

    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 ( ) 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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    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 ( ), your code CORRECTLY excluded the "new stuff" by redirecting it to y.com first!
    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

    Quote Originally Posted by dklynn View Post
    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.
    Yep, got that covered too

    Quote Originally Posted by dklynn View Post
    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 \).
    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.

    Quote Originally Posted by dklynn View Post
    Unfortunately, I can see no other problems (except that you've not retained/displayed the !-f and !-d which is typical of WP)
    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).

    Quote Originally Posted by dklynn View Post
    In other words, being beaten in the past ( ) 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!
    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.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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 )!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    In regards to the !-f and !-d, if I did this
    Code:
    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?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •