Gotcha - drupal clean URL vs default

I have the following line in a module:


      $path = url(drupal_get_path('module', 'my_module') . '/images/my_image.png');

This ended up being embedded in an image tag. However, in two separate systems the image was displayed in one and not the other.

The difference being:


<img alt="This is a my image" src="/?q=sites/all/modules/examples/my_module/images/my_image.png" title="This is the title" />

and


<img alt="This is a my image" src="sites/all/modules/examples/my_module/images/my_image.png" title="This is the title" />

For the first of these the ?q= causes the link to break.  The difference between the two sites is that one has clean URL's enabled and the other does not.  Enabling clean URL's will get rid of the ?q=.

To enable clean URL's in Drupal 7 follow the instructions at Enabling Clean URLs in Drupal 7.

If it is not possible to turn on clean URL's then the path can be cleaned up manually using preg_replace:


      $path = url(drupal_get_path('module', 'my_module') . '/images/my_image.png');
      $path = preg_replace('/\/\?q=/', '', $path);