2

I've found that the following two URLs will show the same content:

http://example.com/blog/
http://example.com/blog/index.html

In the case of the first URL, it appears to pick up the index.html from the blog pseudo-folder. I guess this is because I have index.html configured as my index document.

However, the browser is not redirected. This means I end up with two different URLs being hit for the same content. From the point of view of analytics and other things this is a pain.

Can I make S3 redirect with a 301 to index.html? Or the other way around?

4
  • What about... Don't create links to /blog/index.html. link to /blog/. (with the trailing slash, otherwise S3 will send a 301 to append it.) Showing the index document implicitly and explicitly is essentially standard web server behavior. Sep 19, 2015 at 4:08
  • "Showing the index document implicitly and explicitly is essentially standard web server behavior." -> can you provide a reference for that? I'm interested... Sep 21, 2015 at 8:41
  • Navigating to /blog redirects to /blog/ so that relative paths of documents under "/blog/" work correctly as links. The subsequent request for /blog/ then displays the content of /blog/index.html (implicit) without it being reflected in the address bar. Navigating to /blog/index.html shows the same content (explicit). I can't provide a reference, just anecdotal observations of 18 years of working with web servers... Apache, Nginx, IIS, S3 with static hosting, and any number of relatively minor players... when an index/default document is defined, that's what they do. Sep 21, 2015 at 12:48
  • Hmmm... not so sure. That's an implementation detail of the server, and the server could easily be made to work in a different way (mod script for Apache, or whatever). What I meant by a reference was a citation from one of the the HTTP RFCs or similar. Sep 25, 2015 at 10:25

2 Answers 2

3

Firstly, make sure you are only linking to one version of the URL, whether it is /blog/ or /blog/index.html (I'd prefer the former since it's shorter and simpler).

The best solution with regards to SEO would be to use a canonical tag. Put this inside the <head> tag:

<link rel="canonical" href="http://yoursite.com/blog/">

That means that when search engines see both URLs they are treated the same, and ensures visitors coming from a search engine will hit /blog/ and not /blog/index.html.

If you have links from other sites you may not be able to control where the visitors land, so for analytics purposes it's probably still worth redirecting the user.

I don't know if you have any kind of server-side rules in your situation given that it's a static site, but if you can use .htaccess you should do something like this:

Redirect 301 /blog/index.html /blog/

Finally, if all else fails then redirecting the user with JavaScript is fine:

<script>
if (window.location.href.indexOf('index.html') != -1)
    window.location.href = '/blog/';
</script>
1
  • Thanks, I followed most of this. Unfortunately the .htaccess redirection is not possible. I have changed all links to /, and added both a redirection and a link rel=canonical to the index.html pages. I also had to migrate all my Disqus comments. I won't mark as answered, because the question isn't really answered, but thanks. Sep 25, 2015 at 13:47
0

place in your /blog/index.html on the top, before opening < head>

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location:http://www.example.com/index.html");
?>

so you get the only one url: with index.html on the end - all tries to come into /blog/ will be redirected to /blog/index.html

On hosting, which doesn't support PHP, use a Javascript:

<script type="text/javascript">
<!--
window.location = "http://www.example.com/blog/index.html";
//–>
</script>
2
  • I'm not using PHP, this is a static site hosted on S3. Sep 21, 2015 at 8:41
  • 1
    i edited the post
    – Evgeniy
    Sep 21, 2015 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.