Hi,

I have been converting our old static web pages into Drupal and have encountered a problem when I turned on clean urls. I initially did all development using the ?q=node formats and all files that I uploaded into the drupal sites/default/files/ folder were linked on pages using the following html:

somefile.pdf

These links worked just fine when using unclean urls. When I converted to clean urls, all the drupal pages, books and links worked just fine and the ?q=node/12 changed to node/12 as expected. But now the links do not work. The clean url converst them to node/sites/default/files/somefile.pdf which then brings up the page

Page not found
The requested page could not be found.

I tried adding the "ErrorDocument 401 "Unauthorized" to the .htaccess file in the files folder, but it did not affect anything. Do I need to add a different RewriteRule for file references?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Any help on this would be greatly appreciated.
Thanks,
Matthew Cioffi

Comments

cog.rusty’s picture

This is a known problem, a natural result of clean URLs

- When you are on http://example.com/?q=node/15 and the browser sees a relative "files/filename" path it resolves it as http://example.com/files/filename. The ?q= part does not pretend to be a path, so the browser does not use it.
- When you are on http://example.com/node/15 and the browser sees a relative "files/filename" path it resolves it as http://example.com/node/15/files/filename because it considers node/15 as a real path.

So, when you use clean URLs the file links must have a front slash (and possibly a path if Drupal's home page contains an URL path). The browser sees the absolute path "/files/filename" and resolves it using the bare domain name. You need to edit all the file paths in the content and add a front slash.

Alternatively, try the http://drupal.org/project/pathologic module which tries to be smart about it (or one of the other modules mentioned there).

cioffi’s picture

Thanks for that piece of advice. I must of missed it in reading about the clean URLs.

Jānis Bebrītis’s picture

front slash doesn`t solve it for me. i also added base_path() and then href even looks like ..?q=/drupal6/sites/all/files... and link to images in file folder are still broken.

help?

cog.rusty’s picture

Why did you need ?q= for the path of a real file. It doesn't make any sense. Maybe you need to describe exactly what you are doing and where.

Jānis Bebrītis’s picture

there is this module, prog_gallery, and I have a line like this

$album .= l('<img src="'. base_path() . $entry->images['thumbnail'] .'" alt="'. $entry->body .'" />', $entry->images['_original'], array('html' => TRUE, 'attributes' => array('rel' => 'lightshow[gallery][' .$title. ']'))) .'<br />';

and "$entry->images['_original']" (sites/default/files/images/randomimage_0.thumbnail.jpg) generates link "http://localhost/drupal6/?q=sites/default/files/images/randomimage_0.thu..." which ain`t right.

could be i`m doing it wrong? i also tried to replace lt with actual html code, but $title content is too complicated to parse it on my own.

cog.rusty’s picture

I think the difficulties come from using the l() function which by default is intended for internal Drupal paths and not for real file paths. But maybe you can make it work anyway. Try replacing the link path

$entry->images['_original']

with

'http://' . $_SERVER['HTTP_HOST'] . base_path() . $entry->images['_original']

so that it won't be considered as an internal link and ?q= won't be used.