diff --git a/middleman-core/features/directory_index.feature b/middleman-core/features/directory_index.feature
index 99dbe60e..c1b8f2ef 100644
--- a/middleman-core/features/directory_index.feature
+++ b/middleman-core/features/directory_index.feature
@@ -27,12 +27,12 @@ Feature: Directory Index
And the file "regular/index.html" should contain "Regular"
And the file "evil spaces/index.html" should contain "Spaces"
-
+
Scenario: Preview normal file
Given the Server is running at "indexable-app"
When I go to "/needs_index/"
Then I should see "Indexable"
-
+
Scenario: Preview normal file with spaces in filename
Given the Server is running at "indexable-app"
When I go to "/evil%20spaces/"
@@ -42,7 +42,7 @@ Feature: Directory Index
Given the Server is running at "indexable-app"
When I go to "/a_folder/needs_index/"
Then I should see "Indexable"
-
+
Scenario: Preview ignored file
Given the Server is running at "indexable-app"
When I go to "/leave_me_alone/"
@@ -69,14 +69,13 @@ Feature: Directory Index
And the Server is running at "indexable-app"
When I go to "/link_to/"
Then I should see 'link_to: Needs Index'
- Then I should see 'explicit_link_to: Explicit'
+ Then I should see 'explicit_link_to: Explicit'
Then I should see 'unknown_link_to: Unknown'
Then I should see 'relative_link_to: Relative'
Then I should see 'link_to_with_spaces: Spaces'
When I go to "/link_to/sub/"
Then I should see 'link_to: Needs Index'
- Then I should see 'explicit_link_to: Explicit'
+ Then I should see 'explicit_link_to: Explicit'
Then I should see 'unknown_link_to: Unknown'
Then I should see 'relative_link_to: Relative'
Then I should see 'link_to_with_spaces: Spaces'
-
diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb
index 5ce84523..eabd3e8e 100644
--- a/middleman-core/lib/middleman-core/util.rb
+++ b/middleman-core/lib/middleman-core/util.rb
@@ -247,7 +247,16 @@ module Middleman
current_source_dir = Pathname('/' + this_resource.path).dirname
url_path = current_source_dir.join(url_path) if url_path.relative?
resource = app.sitemap.find_resource_by_path(url_path.to_s)
- resource_url = resource.url if resource
+ if resource
+ resource_url = resource.url
+ else
+ # Try to find a resource relative to destination paths
+ url_path = Pathname(uri.path)
+ current_source_dir = Pathname('/' + this_resource.destination_path).dirname
+ url_path = current_source_dir.join(url_path) if url_path.relative?
+ resource = app.sitemap.find_resource_by_destination_path(url_path.to_s)
+ resource_url = resource.url if resource
+ end
elsif options[:find_resource] && uri.path
resource = app.sitemap.find_resource_by_path(uri.path)
resource_url = resource.url if resource