Gotcha - drupal clean URL vs default

×

Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2405 of /data/htdocs/g/giannelli/web/includes/menu.inc).

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);