Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule for Domain Move

Need help with mod_rewrite for WordPress blog move.

         

gbrown442

7:28 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



Hi,

I'm looking for some assistance with the move of a WordPress blog to a WordPress Network site. Any help would be great. The currently URL looks like this:

http://www.sitename.com/YYYYMMDD-the-title-slug/


To:

http://www.newdomain.com/site-name/YYYY/MM/DD/the-title-slug/


Just to make sure:

YYYY - is the year
MM - month
DD year

Thanks!

lucy24

7:47 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])-([^/]+)/ http://www.example.com/site-name/$1/$2/$3/$4/ [R=301,L]

You said "Rewrite", but if you're going to a different domain you don't have much choice about redirecting. That's assuming "the-title-slug" is variable while "site-name" is constant. When something is the same before and after, you don't need to capture it; just repeat on both sides. You're allowed nine captures.

If your server-- the old one-- will let you, say \d instead of [0-9]. Saves three bytes per digit and is easier to read. I don't think Apache allows the (\d{4}) form which is even more concise.

gbrown442

7:52 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



@lucy24 Thanks for the quick reply. I'm going to test this right away, everything makes since.

I have another old permalink which I'm having trouble with, the biggest issue with this one is that it doesn't have the DD (day) value in the old permalink, but the new one does:

http://www.sitename.com/YYYY/MM/the-title-slug.html/


Goes to same as above..

http://www.newdomain.com/site-name/YYYY/MM/DD/the-title-slug/ 


Thanks!

g1smd

7:58 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the "DD" data is not in the original URL request, there is NO way for mod_rewrite to add it.

You will have to rewrite the "incorrect" URL request to a special PHP script that will look up the DD data in the database and then send the "301" and "new URL" HEADER from there.

gbrown442

8:03 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



@g1smd I had a feeling that was going to be the case. Do you have any suggestions going about creating a PHP script for that?

Thanks

gbrown442

8:12 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



@lucy24 I've added this code to my .htaccess and it doesn't seem to be working correctly:

Options +FollowSymLinks 
RewriteEngine on
RewriteRule ([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])-([^/]+)/ http://www.rantsports.com/new-york-islanders/$1/$2/$3/$4/ [R=301,L]


It should be pointing this URL:

[thelonelyislanders.com...]

To the new destination, right?

gbrown442

8:32 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



@g1smd I'm thinking through the the code for a custom script. This is my logic of it, does it make sense?

I already know of a code that can break up the URL to get the WordPress slug. Once we have that, we query the Database for the post with the slug. Once we have that, we should be able to piece the parts together to make the redirect URL to the new location on RantSports.com, something like this:

1. Get Slug
2. Query DB for Slug
3. Grab Date from DB
4. Turn date into YYYY, MM and DD
5. Put Back together as: rantsports.com/team-name/yyyy/mm/dd/slug/
6. Redirect

g1smd

8:40 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes that's the sort of thing you require.

gbrown442

8:50 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



Thanks!

gbrown442

9:46 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



This part still isn't working correctly, suggestions?

Options +FollowSymLinks
RewriteEngine on
RewriteRule ([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])-([^/]+)/ [rantsports.com...] [R=301,L]

It should be pointing this URL:

[thelonelyislanders.com...]

To the new destination, right?

lucy24

10:02 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're an Islanders fan and you're worried about... Oh, never mind.

Overdue question: where is the htaccess file located? If it isn't in the top level of the old domain,* it will never be seen by the computer that needs to see it. Oh, and just to cover all bases and mix all metaphors --if this is any kind of shared hosting, make sure you don't have to jump through any special hoops when redirecting to a different domain. Even something as basic as clicking another button somewhere.


* Unfortunately olddomain dot com isn't a reserved name. It would be awfully useful to have two possible names.

gbrown442

10:18 pm on Jul 27, 2011 (gmt 0)

10+ Year Member



I'm actually a Blackhawks fan. :)

But yes, the htaccess in at the top level. I just checked it again though and for some reason every time I save the .htaccess file its removing the redirect rules...

Also, will those rules redirect these to links to their respective places:

[thelonelyislanders.com...]
[thelonelyislanders.com...]

To

[rantsports.com...]
[rantsports.com...]

Thanks

g1smd

11:59 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use example.com to stop forum auto-linking.

gbrown442

3:51 am on Jul 28, 2011 (gmt 0)

10+ Year Member



Will do.

I'm working on a PHP script like the one we talked about above for redirection. Here's the script, the only part that's not working so far is passing a the $new_url through the Header area:

<?php

$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_slug = $post_obj->post_name;

$post_date = $wpdp->get_results("SELECT post_date FROM $wpdb->posts WHERE post_title = $post_slug");

list($y, $m, $d) = explode('-', substr($post_date, 0, 9));

$new_url = 'http://www.example.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".$new_url." );

?>

lucy24

4:04 am on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I just checked it again though and for some reason every time I save the .htaccess file its removing the redirect rules...

Then you will definitely need to have a chat with your host-- or read the WordPress fine print-- because htaccess files do not self-destruct.

Now look at your most recent post. g1 gets very cranky if he has to tell you more than once to use example.com. In these forums, all complete urls get auto-converted into clickable but unreadable links. There is special code to exempt www.example.com. (In real life, it is a reserved name that will never belong to an actual domain.) I am a slow typist. We overlapped. So what you're trying to say is:

http://www.olddomain.com
and
http://www.olddomain.com/feed/
[I cheated in constructing those names. The same trick works in php/bb2 forums, but php/bb3 is too smart.]

should turn into

http://www.example.com/new-york-islanders/
and
http://www.example.com/new-york-islanders/feed/

No problem, once you've sweet-talked your host. What you're doing is capturing everything after the domain name (www.olddomain.com) and sticking it onto the new name, which begins

http://www.example.com/new-york-islanders/

gbrown442

4:11 am on Jul 28, 2011 (gmt 0)

10+ Year Member



Ok, got the $new_url working, but the script isn't working. Here's what I currently have:

<?php

$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_slug = $post_obj->post_name;

$post_date = $wpdp->get_results("SELECT post_date FROM wp_posts WHERE post_title = '$post_slug'");

list($y, $m, $d) = explode('-', substr($post_date, 0, 9));

$new_url = 'http://www.example.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".$new_url."" );

?>


And this is the error:

Fatal error: Call to a member function get_results() on a non-object in /home/letsgetm/public_html/wp-content/themes/lgm_rantsports/single.php on line 8

gbrown442

5:07 am on Jul 28, 2011 (gmt 0)

10+ Year Member



Ok, got the code above working. Had a typo, but this is the result I'm getting:

http://www.example.com/TEAM-NAME/Array/thoughts-from-england/