How to move your Drupal site from old domain to new one

This tutorials outlines common steps I usually take when changing from old domain to new domain. I assume you want to move from www.olddomain.com to www.newdomain.com

  1. Add the domain and any subdomains (for example "m.") in the Cpanel. Then create the email id for this domain. No need to change the mysql part.
  2. Move all domain files( including GWT/BWT verification files) from old domain directory to new one
  3. Setup this .htaccess in the old domain:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} !^m\.
    RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [L,R=301,NE]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST} ^m\.
    RewriteRule ^ http://m.newdomain.com%{REQUEST_URI} [L,R=301,NE]

    Remove all files except for .htaccess and GWT/BWT verification files.

  4. In the old domain copy the Google's and Bing webmaster verification files.
  5. In GWT add both domains : http://www.newdomain.com and http://newdomain.com. Setup redirect from http://newdomain.com to http://www.newdomain.com as in default Drupal .htaccess by uncommenting the lines. Verify them all.
  6. Go to admin/config/search/xmlsitemap/settings and check/change the Default Base URL in "Advanced Settings". In the main site sitemap see that links are correct and check it by clicking them.
  7. Go to admin/config/system/site-information and change your email id
  8. Issue site change request in GWT and BWT. Unlike Google's, you can't cancel the "Change of Address" in Bing.
  9. Submit sitemap for the new domain for both BWT & GWT.
  10. Check your mobile site settings. Create the "m." subdomain for new domain
  11. Search in your hosting account folder all occurrences of old domain(command: egrep -rl "www.olddomain.com") and change it to new one. Do the same in your development system too in all the files.
  12. Update the site in social networks like Facebook, Twitter and site uptime monitoring tools

Reading/writing files programmatically in Drupal 7

Reading/writing files programmatically in Drupal 7

There are two types of files in Drupal. Managed and Unmanaged.

Managed Files

The file information is stored in file_managed table and files can be linked to nodes and users

Schema of file_managed table

It contains following columns

  • fid
  • uid
  • filename
  • uri
  • filemime
  • filesize
  • status ( whether temporary or permanent)
  • timestamp

Uploading Files

Associating uploaded files with the nodes etc is taken care through table named filed_data_field_file_xxxx where xxxx is the unique name of the particular cck field.


It is default scheme of site which could be public or private

Saving a file

Use file_save_data($content,$destination_uri=null,$replace=FILE_EXISTS_RENAME)

If you've a relative/absolute file path - convert it to URI using file_build_uri($path) function.

If you've an URI then convert it to actual path to access using PHP using drupal_realpath($uri)

Attaching Files to Node

Use this sample code to attach a file:

  1. //Attach an empty serialized file
  2. if ($form_state['values']['field_serialized_data'][$form['field_serialized_data']['#language']][0]['fid'] == 0) {
  3. //create the serialized file
  4. $time = time();
  5. $filename = 'public://' . "disavow/$time.sr";
  6. $file_temp = file_save_data(serialize(array()), $filename, FILE_EXISTS_RENAME);
  8. if (!$file_temp) {
  9. form_set_error(null, "$filename could not be saved!");
  10. watchdog('ANU_DISAVOW', "$filename could not be saved!", array(), WATCHDOG_ERROR);
  11. return;
  12. }
  14. $form_state['values']['field_serialized_data'][$form['field_serialized_data']['#language']][0] = array(
  15. 'fid' => $file_temp->fid,
  16. 'display' => 1,
  17. 'description' => '',
  18. );
  19. }

In case in a CCK file field file is not attached then fid will be 0

Accessing Attached Files with Node

Something like this:

  2. $fileobj = file_load($form_state['values']['field_serialized_data'][$form['field_serialized_data']['#language']][0]['fid'] );
  4. $filepath = drupal_realpath($fileobj->uri);
  5. $file_content_arr = unserialize(file_get_contents($filepath));

Generating Temporary File

This api function: drupal_tempnam($directory, $prefix) will generate it. It can accept/return both in URI/filepath format.

Preventing Webforms Submissions from bots in your Drupal site

Tired of incessant spam webform submissions from bots?

All have unique quality : they contain links of sites they are promoting or may contain junk links - links of sites which don't exist.

This module( Drupal 6)actually consists of two things:

  • Prevent anyone from inserting links in the body. You can configure $MAX_LINKS_ALLOWED value yourself. If it's value is 0 then it means no links will be allowed.
  • Prevent anyone from inserting even single link in the comment form. Also the "Home page" field will be disabled from view so that there is no scope of entering links through comment form.

Take care to name the webform(Edit->Form Components->Field Key) textarea component to receive the comments to be named as 'comments_questions_feedback', if it is not then replace this text in the code below to the name you've used.

Just create a folder name "anu_disable_http" in site/all/modules and place these two files and then enable them in your modules( path: admin/build/modules)



function anu_disable_http_form_alter(&$form, &$form_state, $form_id) {

    if (
$user->uid == 1) {

    if (
$form_id == 'comment_form') {
$form['#validate'][] = 'anu_disable_http_validate_comments';

        if (isset(
$form['homepage'])) {
$form['homepage']['#access'] = FALSE; //disable homepage
    } else if (
preg_match('/^webform_client_form_[0-9]+/', $form_id)) {

$form['#validate'][] = 'anu_disable_http_validate_webform_comments';
    } else {

anu_disable_http_validate_comments($form, $form_state) {
$pattern = '/\bhttps?:\/\//i';
    if (
preg_match($pattern, $form_state['values']['comment'])) {
form_set_error('comment', "Please remove http:// or https:// from links, we will add them if required");

anu_disable_http_validate_webform_comments($form, $form_state) {
$val = $form_state['values']['submitted']['comments_questions_feedback'];

$pattern = '/\bhttps?:\/\//is';
//see if more than one http is found then disallow
if (preg_match_all($pattern, $val) > $MAX_LINKS_ALLOWED) {
form_set_error('submitted][comments_questions_feedback', "Please remove http:// or https:// from links, we will add them if required");



; $Id: anu_disable_http.info,v 1.1 2012/09/26 09:08:13 D14 Exp $
name = Anu Disable http in comments
description = to prevent spam
files[] = anu_disable_http.module
core = 6.x
package = custom
; Information added by drupal.org packaging script on 2010-11-12
version = 1.0
core = 6.x

How to delete the attachement files programmatically in Drupal

How to delete the attachment files pro grammatically in Drupal(D7 here)?

Somehow I've worked on this code but I'm not sure whether it is fool proof. But at the moment it is working for my site.

In the code field_file_attachment_s_ is the name of attachment field for my site. Assuming there could be multiple files which can be attached to this field. I've yet to test the multiple attachment file deletion feature in the code.

If you've got any better code then I'll be happy to have it.

Thanks for reading this!

= node_load($nid);

//delete the file field 
        // Get the language for the file field.
$lang = field_language('node', $node, 'field_file_attachment_s_');
// load the file

for ($i = 0; $i < count($node->field_file_attachment_s_[$lang]); ++$i) {
$file = file_load($node->field_file_attachment_s_[$lang][$i]['fid']);
// unset the field for the node
if ($file) {
// delete file from disk and from database

How to delete nodes in bulk, content types and fields in Drupal 7

Here is what I want. I've copied my site contents to another site name since I'm making a new site. At least all the settings, modules and optimizations will be carried forward in the new site.

But I don't want the custom content types, their nodes and unused fields. I want to remove those fields from my database. Why leave them dangling there forever? So for Drupal 7 I'm listing here how to delete nodes in bulk,content types and fields

Delete the Nodes in Bulk

Firstly use this Drupal module. It is for Drupal 7 but not for 6! Install it and simply go to Administer -> Content and delete the nodes selectively.

Otherwise you can delete them using this Drush command:

Delete all nodes, nodes of a specific content type or users.



    drush delete-all article             Delect all article nodes.
    drush delete-all all                 Delete nodes of all types.
    drush delete-all --reset             Delete nodes of all types, and reset node, revision and comment counters.
    drush delete-all users               Delete users.  

    --reset                              Reset counter for node, revision and comment tables.
    --roles                              pick roles  

    Aliases: da

Delete the Content Type

So you've deleted the desired nodes but also want now to delete the custom content types. In Drupal 7. Just type in this url : "admin/structure/types/manage/[custom-content-machine-name]/delete" and it'll ask your confirmation to delete it from Drupal database. Replace underscone in the machine name with hypens(-).

Deleting Fields

So you want to delete the unused fields from the database? Better delete them. That'd rid some tables dedicated to those fields.

While browsing of you happen to find field tables like "field_delete_data _??" or "field_deleted_revision_??" then don't try to delete them. They were created when you deleted the custom content types. Just run Cron once and they'll be automatically gone.

Using Drush field-delete

Simply use the safe command of Drupal Drush to delete undesirable fields:

Delete a field and its instances.    
field-delete city                         Delete the city field and any instances it might have.
field-delete city --bundle=article        Delete the city instance on the article bundle 

Arguments:   field_name                   Name of field to delete.

Options:   --bundle                       Only delete the instance attached to this bundle. If omitted, admin can choose to delete one instance or whole field. 
  --entity_type        Disambiguate a particular bundle from identically named bundles. Usually not needed.                             

Deleting content type confirmation in Drupal 7

Creating Slideshow of local & remote files in Drupal in a node

Here is what I want. I want to show all images linked to an image field which can contain unlimited values in a slideshow in the bottom of the web page.

I'm showing apartment ventures so I want to show the project photos in slideshow of it's various designs and facets.

I also have another constraint that to save bandwidth, disks pace of my site, we should be able to specify external images rather than downloading them on our hard disk. It can be mix of local & remote images(http: linked ones). I prefer using the urls rather than downloading the photos and remember to delete them later.

After some work I could find out how to do it.

Good news is that using the same method we can show image in a field which can be locally stored or linked externally to another website.

1. First install Embedded Image Field and Remote Stream Wrapper modules. Enable them. Also install Field Slideshow module.

2. Create a field of type image. But select the widget as "Media file selector". Remember it was added by the Embedded Image Field

First click Image on the Widget place. Select Media file selector in the widget(

Step 2

3. Go to the MANAGE DISPLAY tab, locate the field click the Format field of the field and select Slideshow. Save it. Now you can configure Slideshow options by clicking the wheel shaped tools icon to the far right of the field and then saving the settings.

Step 3

So this way I could show local and remote image files in slideshow.

Speeding up Drupal on Microsoft Windows

For 7 months I know how slowly the Drupal 7 worked on the localhost(Windows 7).

I tried all sorts of optimization but nothing worked.

I used Devel module to see the mysql query log. And I always saw that it was mysql which was the bottleneck.

The final turn around came when I hit upon this mysql optimized config. I want to salute this person to have ended all my woes. Now my localhost Drupal 7 is pretty fast!

I suggest you start with this config and then ticker is one by one. If you change innodb_log_file_size then you must remove ib_logfile from mysql data directory on Linux or Windows before you restart mysql otherwise it will throw an error.

Hardware Config

I've a Intel i3 530, 8 GB RAM, 1 hard disk.


Here is the ultimate Drupal optimization blog. But remember the above optimized config file will improve site speed on Windows by 99%.

Here is simple and general check list I've prepared for the production server:

  1. In mysql set cache buffer and cache query to highest value
  2. Set up APC (It crashes on my Windows so I don't use it, but do use it on Linux)
  3. Turn on Drupal Caching in Performance tab
  4. Turn on Views caching( query etc)
  5. Set mod_deflate in Apache
  6. Turn on Keepalive in Apache but if memory is less then turn it off. KeepAlive is off by default. To on it go to WHM->Apache Configuration and turn it on. More at here.
  7. Try to use DSO php handler in CPanel. But then you'll need to do chmod 777 in the public_html and all files/folders below it.
  8. Turn off mailmain, spamassisin( in 2 places, one in Tweak settings), IMAP. This will heavily reduce VPS memory usage.

Here is the approx mysql inno db settings which I'm using:

  innodb_buffer_pool_size = 1024M

  innodb_flush_log_at_trx_commit = 0

  query_cache_size = 4M

  innodb_flush_method = O_DIRECT

  innodb_log_file_size = 32M    
#( before restart run this command else mysql error will occur : rm /var/lib/mysql/ib_logfile* )

  innodb_log_buffer_size = 4M

  join_buffer_size = 8M

  sort_buffer_size = 3M

  query_cache_limit = 256K
  tmp_table_size = 64M

  max_heap_table_size = 64M

  table_cache = 4096

  thread_cache_size = 128

  wait_timeout = 300

  table_definition_cache = 4096

  table_open_cache = 16384


Last but not least backup up you site, database before you do anything. You must be able to restore it! To backup mysql, you can simply backup the data folder in Windows under mysql. You can also use drush arb commad which is easy.

Syndicate content