20

Ok, maybe a pretty dumb question but I can't find an answer on Google.

I am coding my site by hand. How do I make the index.html disappear from the url's? Do I use a piece of code in my html? Do I have to change my href's in my files?

Hope you guys can help!

EDIT: I've tried this with a .htaccess file

RewriteEngine On RewriteRule ^index\.html$ / [R=301,L] RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

It does work, but all my links aren't working anymore. I discovered I had to remove all the index.html from the href's in my documents. But that would be a lot of work. Is there an other code for .htaccess that just hides index.html?

David Hakkert
  • 669
  • 3
  • 10
  • 25
  • You could redirect the user if location.href ends with index.html? – ReeCube Mar 18 '14 at 13:36
  • You can do that with .htaccess and rewrite url. See : http://stackoverflow.com/questions/5730092/how-to-remove-html-from-url – Nuz Mar 18 '14 at 13:38
  • 1
    it is called mod rewrite and there is tons of stackoverflow question about it -> http://stackoverflow.com/search?q=mod+rewrite+index.html – davidkonrad Mar 18 '14 at 13:38
  • 2
    @davidkonrad good resource, but if the user doesn't know it's called mod rewrite, they would never find these answers :-) – TylerH Mar 18 '14 at 13:57
  • @TylerH, yes you are right, sorry user1737979, no offense!! – davidkonrad Mar 18 '14 at 14:15
  • No problem. I didn't know it was called mod rewrite. I added a htacces file to my site with the code in the question above. But that gave me another problem... – David Hakkert Mar 18 '14 at 14:27
  • If you see my answer it only affects directory index files and accesses to links containing index.html still gives the correct response. – vogomatix Mar 18 '14 at 16:01

8 Answers8

34

A SIMPLE WAY TO DO THIS in Html:

(example in my case is a simple dual language site)

If your link looks like this:

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

You should change it to this:

<a href="/">Homepage</a>

If trying to link to another folder in your directory, like is my example:

<a href="en/index.html">English language</a>

You should change it to this:

<a href="en">English language</a>

Notice that "/" goes back to your root directory and automatically selects index.html, so that is why I used "en" for the English language site, because the name of the folder in that case is "en". You should also make sure that you have index.html in your English language folder, and not index-en.html.

Darko Atanasov
  • 475
  • 4
  • 4
  • 1
    If I have index.html and page2.html in the root directory, can the homepage link to the other page with `Next`? I don't want the url to read /page2.html, but just /page2. – Matt Jul 17 '17 at 15:15
  • 1
    This answer worked great for me. Thanks. For above comment on localhost, I don't think it works when you're testing locally. When I was testing in localhost, "/" displayed a list of files and folders contained in the project. But once I published the website, "/" worked just as intended. – L.U. Sep 02 '19 at 11:39
3

Apache has .htaccess files and mod_rewrite, In your .htaccess file, set:

DirectoryIndex index.html

You can also set this up in the Apache site config files too

You can specify a list of filenames, so if it doesn't find the first it moves to the next.

IIS has .config files

vogomatix
  • 4,856
  • 2
  • 23
  • 46
  • 1
    would be great if you wrote the extra 2 sets of lines as a complete example. – JGallardo Feb 23 '18 at 09:12
  • There are no extra lines for the .htaccess file. There are extra lines for the config files, which is why I provided links to them – vogomatix Feb 25 '18 at 13:11
0

I think this is configured in IIS when you deploy the site, I'm not to sure on it but I'm sure you can specify a start point that your URL will use when you just enter the UL, that implies the Index.html page.

Sorry I'm not too helpful here, hopefully it will point you in the right direction.

Often these things such as Apache or IIS have this set up already, and it looks for the Index.html, Index.php first when you just put your URL in.

hatef
  • 5,491
  • 30
  • 43
  • 46
Joe Harper
  • 470
  • 2
  • 11
0

mod_rewrite module is responsible for all the rewriteEngine. Check on your server whether is module is present and enable.

murtuza hussain
  • 458
  • 1
  • 7
  • 18
0

You need to create a file called '.htaccess' in the root directory of your site containing the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

And then make sure all the links on your site don't contain '.html' at the end, e.g.:

<a href="/Home.html">

should be replaced with:

<a href="/Home">

Hope this helps!

if you dont find .htaccess, you just need to create a new file using your text editor the same way you would any other html or css file, but save it as simply '.htaccess'

And save it into the root directory, so the same folder that you have your index.html file.

Ashish Yadav
  • 133
  • 1
  • 8
0

Great SEO idea! This is similar to nginx redirect loop, remove index.php from url and Apache .htaccess to hide both .php and .html extentions, as well as mod_rewrite not working htaccess — the idea here, for both Apache's mod_rewrite and nginx ngx_http_rewrite, depends on the differences between the external and internal URLs — in order for the redirect from /index.html to / work, yet not cause redirect loops when actually serving index.html, you gotta make sure you only issue the redirect for external index.html requests, but never for internal ones; this can only be accomplished by looking into the actual request_uri.


Here's the code for nginx ngx_mod_rewrite:

index   index.html  index.txt;
if ($request_uri ~ "^(.*/)index\.(html|txt)$") {    return  301 $1; }

On Apache's mod_rewrite, it'll be something like the following:

RewriteEngine   on
RewriteCond     %{REQUEST_URI}      ^.*/index\.html$
RewriteRule     ^(.*/)index.html$   $1      [R,L]

Related:

References:

cnst
  • 25,870
  • 6
  • 90
  • 122
-1

Change the link that goes to your homepage to the website address. You may have:

<a href="index.html">Link Here</a>

Change that to:

<a href="http://www.domain.com">Link</a>

Or try this

AlexanderFT
  • 81
  • 1
  • 2
  • 13
  • 1
    I saw this solution earlier. But then I have to publish my site en copy all the url's back to the file... A lot of work... I hoped there was a simple solution. – David Hakkert Mar 18 '14 at 13:59
  • Don't know for shure if the .htacces wil work because you are building page after page. I think you need to change the links. If you use programs like dreamweaver it is easy to change just do control+f and search for you link and fill in the replace by form and it wil replace all the links with that name. – AlexanderFT Mar 18 '14 at 14:28
-4

Simply don't type the index.html in your browser and don't advertise it as such.

You can set the 'default document' on the web server (whichever you happen to be using) to serve 'index.html' in the absence of a file part. This way, someone going to http://www.mysite.com would be served http://www.mysite.com/index.html

Steve Martin
  • 1,632
  • 10
  • 19
  • 1
    I know... But when I link to a page from my homepage and I click the link, index.html appears. Simple solution is to use the url in de href. But, that would be a lot of work... – David Hakkert Mar 18 '14 at 14:03