3

I have changed the domain of my website from www.mysite.com to mysite.net. I upload the site to the new domain and all is fine, but I have more than one thousand nodes, and in almost all the nodes I have a link to another node on the site.

The link are absolute so they have this form: www.mysite.com/node/22 and I need to change all nodes to the new domain.

Changing 1,000 links manually is too much. How can I do this automatically??

1
  • 1
    Redirect all traffic from www.domain.com to domain.com with your .htaccess is an option?
    – rreiss
    Commented Aug 20, 2013 at 16:11

5 Answers 5

8

Why are you using absolute links? If you were using relative links, of the form /node/22, the problem would not arise.

I am assuming you are using Drupal 7. The answer for D6 would be a little more complex.

If the links are in the node body, and if you have direct access to your database, you can update the text by running a query on the field_data_body and field_revision_body tables.

First, back up your database in case this goes wrong.

Then run the following query on your tables:

UPDATE field_data_body SET body_value = REPLACE( body_value, 'a href="http://www.mysite.com/','a href="http://mysite.net/');

Repeat with the field_revision_body table.

This should change all references to the old domain with the new domain.

If you have links in the comments, you might also want to do field_data_comment_body and field_revision_comment_body, scanning for references in the comment_body_value field.

In the event the links are being generated automatically by a text filter, change the REPLACE patterns accordingly.

3
  • 3
    1) Without http:// before domain name browsers should treat links as a relative. If they are absolute, there is supposed to be protocol in them. 2) It could be better to just get rid of the domain name instead of updating it, for future. 3) The way you wrote it, it will not work for things like images and embedded media, and might fail for the links created with any kind of wiki-like or bb-like syntax, nor for the ones dynamically created from URLs plainly pasted into node/comment body.
    – Mołot
    Commented Aug 20, 2013 at 8:01
  • @Mołot You are right about the protocol (thank you - was in a hurry). I have updated the answer. The OP specifically asked about links to pages. I assume if he wants to change other links, the pattern would change accordingly. I agree about the relative links.
    – Triskelion
    Commented Aug 20, 2013 at 15:45
  • 1
    Well, 2 and 3 was just suggestions I believe could help future readers, not bugreport on your answer ;)
    – Mołot
    Commented Aug 20, 2013 at 17:44
3

You can use the Pathologic module to correct links in your contents.

2
  • Pathologic is a great module and certainly a solution but it doesn't solve the problem of incorrect paths being stored, it only rewrites them when they are displayed.
    – George
    Commented Mar 23, 2016 at 10:53
  • Pathologic can rewrite your old domain to the new one if you configure your old domain as an "Also considered local" domain. Commented Mar 23, 2016 at 15:42
0

A really nice module to help you do this is Search and Replace Scanner

Or if you prefer something simpler (as a drush command) without the interface you could use Drush Search & Replace

-3

What you could do if you don't want to touch the db is using hook_process_html, so that you use relative path's for all content, scripts and stylesheets:

function MY_MODULE_process_html(&$vars) {
  //use relative path
  $vars['scripts'] = str_replace("http://www.mysite.com/", "/", $vars['scripts']);
  $vars['styles'] = str_replace("http://www.mysite.com/", "/", $vars['styles']);
  $vars['page'] = str_replace("http://www.mysite.com/", "/", $vars['page']);
}
1
  • 2
    This seems like a MASSIVELY inefficient way of doing things.
    – Chapabu
    Commented Aug 20, 2013 at 15:50
-3

Finally I use this http://interconnectit.com/products/search-and-replace-for-wordpress-databases/

Thanks a lot!!

3
  • Hello. Could you elaborate a bit more? Link-only answers usually stops being useful when the linked site goes down or relocates - and your own questions shows it can happen ;)
    – Mołot
    Commented Aug 21, 2013 at 12:10
  • May you expand your answer to say exactly what you did? Link-only answers are not considered answers, on Stack Exchange. Providing more details would help making the question helpful for future readers.
    – avpaderno
    Commented Aug 21, 2013 at 12:11
  • You are in a drupal Q&A page and link to wordpress solutions. Commented Mar 12, 2018 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.