0

I have an apache2 web server, located at /var/www/html. The endpoint is xyz.com and is university-afiliated and managed. The root of mine is supposedly xyz.com/name and it is proxied to by the main server.

When I have:

# index.html
link to href='other.html'
# other.html
link to href='./'

in var/www/html, this only works if the user starts at xyz.com/name/index.html (with the user correctly routed to xyz.com/name/other.html); otherwise, if the user starts at xyz.com/name then they get routed to xyz.com/other.html (which does not exist).

How can I fix this? (prefer HTML + server config only)

user129393192
  • 797
  • 1
  • 8

1 Answers1

0

The issue arises when you don't have a trailing slash.

xyz.com/name/ with a trailing slash is conventionally a directory
xyz.com/name without a trailing slash is conventionally a file

Relative links from xyz.com/name will resolve relative to the root directory xyz.com/ because name is assumed to be a file within the root directory. This could break not only anchors but images and stylesheet links as well.


If name is a directory, some servers will automatically redirect to name/. Since yours doesn't, you could set it up to do so (either directly on the server via httpd.conf or via a .htaccess file in the root web directory.) This will prevent the aforementioned linking issues as well as prevent duplicate content from both URLs (an SEO concern.)

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

More information and explanation can be found in the article "Should You Have a Trailing Slash at the End of URLs?" as well as by performing a web search for trailing slash in URL.



An alternative to redirecting is to use the <base> HTML element in the <head>, which specifies the base URL to use for all relative URLs in a document.

Caution: this can break in-page anchors. Using <base href="/name/"> on xyz.com/name/other.html, the link <a href="#some-id">in-page anchor</a> points to xyz.com/name/#some-id. However, it's not necessary to use <base> in every page, only in index.html.

Tim R
  • 2,622
  • 1
  • 3
  • 19
  • I just tried the rules you specified at `.htaccess` in `/var/www/html` and it does not work. Same issue. I should add that `xyz.com/name` (no trailing slash) originally results in the same location as `xyz.com/name/`, which is `xyz.com/name/index.html`. It is only on attempted relative re-directions (say to `other.html`) that we then see `xyz.com/other.html`. – user129393192 Aug 02 '23 at 04:20
  • The `base` method on the other hand, after the second re-direction (back to `index.html` and to another page) sends you to `xyz.com/name/name/other.html`. – user129393192 Aug 02 '23 at 04:22
  • When updating the .htaccess, it's best to test changes after closing the browser, possibly hitting reload, or even better in a private window. Did you use `` with a leading and trailing slash? `/name/` – Tim R Aug 02 '23 at 05:06
  • Tested in Safari and Chrome on incognito/private windows. I specified `base` exactly as you said. I want to emphasize it's only on the second re-direction where there is a problem. – user129393192 Aug 02 '23 at 14:02
  • What do you mean by "second re-direction"? Are you saying that when you are on `xyz.com/name/other.html` and use the `href='./'` link, it correctly links to `xyz.com/name/` but then the `href='other.html'` link is linking to `xyz.com/name/name/other.html`? – Tim R Aug 02 '23 at 21:40
  • I'm saying that if you start at `/name`, having put `base href='/name'` in `index.html`, then if you go to `other.html` back to `index.html` and back to `other.html`, then that second time you go to `other.html` will send you to `xyz.com/name/name/other.html`, while the first does so correctly. – user129393192 Aug 03 '23 at 00:40
  • You should not have `base href='/name'`. It needs to end with a slash. `` – Tim R Aug 03 '23 at 02:41
  • Sorry, that is what I meant. It did end with a /. – user129393192 Aug 03 '23 at 15:20
  • I can't replicate that unless I leave out the first slash `` – Tim R Aug 03 '23 at 23:13
  • I am not sure then. Thanks a lot for trying to help. I just tested again and with ` – user129393192 Aug 03 '23 at 23:16
  • Make sure you're not missing the closing quotation mark. ;-) Does it link to `xyz.com/name/name/other.html` even when you go directly to `xyz.com/name/` (not back and forth between pages) – Tim R Aug 03 '23 at 23:29
  • I always seem to make typos when speaking to you lol. `xyz.com/name/` always works properly (even without `base`). `xyz.com/name` directs to `xyz.com/other.html` when you click `href='other.html'`. If you add `base`, the only behavior that changes is `xyz.com/name` directs to `xyz.com/name/other.html` the first time, but when you go back via `href='./'` and click the link again, it sends you to `xyz.com/name/name/other.html`. I hope that's clear. – user129393192 Aug 03 '23 at 23:33