Relative links to index.html are absolute

Hi all,

I’m trying to migrate a site to v4, but seem to have hit a dealbreaker with relative links when the target is called index.html, since it that case the links become absolute. For example, in code/index.html.erb I have

<%= link_to 'text', '/index.html', :relative => true %>

In the rendered code I would expect

 <a href="../index.html">text</a>

or

<a href="../">text</a>

but I actually get

<a href="/index.html">text</a>

I get the expected behaviour if the target of the link is called anything but index.html, bizarre!

Am I missing something here?

Thanks in advance!

Jim

1 Like

Did you ever figure this out?

I’m getting the same thing with <%= link_to 'Home', '/' %>.

I’ve tried adding the relative: true option to the link_to, and also as a global configuration in config.rb

I never did, and it’s a deal breaker for me, so I’ll not be upgrading to 4 (and will eventually move to Jekyll I guess).

1 Like

I’m not actually using relative links, so maybe this is way off-base but I would expect relative links to get confused if your link starts with a slash (e.g. /mypage.html) because that is an absolute path.

I don’t have a site with relative links going right now so I can’t try but maybe that helps. Also, I’d suggest elaborating on which pages you are trying to link between because it is a bit unclear from the posts in this thread.

@mehulkar Again, I’m not using relative links but in my navbar when I want to link back to the home page I use <%= link_to 'Home', '/index.html' %> . Since it seems like you are trying to link to your Home it seems like this is more appropriate than a relative link.

@jjgreen If you are trying to link to the index one directory up (as you indicate in your expected anchor tag), then I would think that this might do the trick: <%= link_to 'text', '../index.html', :relative => true %>. Again, can’t confirm this.

Hi @ericpowell

According to the docs the link_to helper uses a relative URL if the argument is absolute and the :relative => true option is given, “[unless it] fails to determine which page the URL provided belongs to”. It seems that this is the case for the absolute paths /index.html and /, although I’m not sure I can see why those particular paths would be a problem.

In my case using an absolute path is not an option, since I build the site locally and upload it to a server where it is not at the URL root location. I don’t want to hard-code links as relative, since then, whenever I move a page I will break all of the links in that page. Middleman v3 handles this conversion of absolute to relative without issue, v4 does not.

Still not sure about the relative link situation but since you say your motivation for relative links is because your URL is not root on the server, perhaps the :http_prefix option could be of use.

This is what I am using on my home server. I have this snippet in my config.rb:

configure :build do
  set :http_prefix, '/demo/mysite/build/'
  ...
  ...
end

Then I access the site through localhost/demo/mysite/build instead of just localhost. Hope I understood your issue correctly.