-1

How can I redirect every index.html file to / For example redirect this URL: https://www.example.com/contact/index.html to https://www.example.com/contact/

Note: I don't want to redirect to the root, I want to redirect to the same directory just remove the index.html Thank you in advance for your time

Ardian
  • 11
  • 1
  • 2
  • 1
    Possible duplicate of [Redirect www.mysite.com/index.html to www.mysite.com/ with .htaccess file](https://stackoverflow.com/questions/8545243/redirect-www-mysite-com-index-html-to-www-mysite-com-with-htaccess-file) – esqew Jan 30 '19 at 15:05
  • Hi @esqew I saw that and it's a different question. I don't want to redirect to the root. I want to redirect to the same directory just remove index.html – Ardian Jan 30 '19 at 15:07
  • Possible duplicate of [Redirect www.mysite.com/index.html to www.mysite.com/ with .htaccess file](https://stackoverflow.com/questions/8545243/redirect-www-mysite-com-index-html-to-www-mysite-com-with-htaccess-file) – it4Astuces Jan 30 '19 at 15:07
  • Hi @it4Astuces I saw that and it's a different question. I don't want to redirect to the root. I want to redirect to the same directory just remove index.html – Ardian Jan 30 '19 at 15:08
  • @Ardian Check this out: https://stackoverflow.com/questions/22481028/removing-the-index-html-from-url – it4Astuces Jan 30 '19 at 15:13

1 Answers1

1

Assuming you are using Apache the following lines in .htaccess file under the /contact folder will do the trick:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/index.html$   %{CONTEXT_PREFIX}/folder/$1/        [R=301,L]
RewriteRule ^index.html$   %{CONTEXT_PREFIX}/folder/    [R=301,L]

where folder is contact in your example.

Earlier I suggested using the following rule for a shortcut of the above two:

RewriteRule ^(.*)index.html$   %{CONTEXT_PREFIX}/folder/$1       [R=301,L]

However, this has an unwanted side-effect of redirecting say something like https://www.example.com/contact/myindex.html to https://www.example.com/contact/my

Note: Please be careful not to cause redirection "loops" like:

xxx/index.html -> xxx/ -> xxx/index.html -> xxx/ ...
FedKad
  • 493
  • 4
  • 19
  • Hi @Fedon Kadifeli Thank you very much for your answer. What if the URL is like this: https://www.example.com/contact/page-1/index.html and to redirect to https://www.example.com/contact/page-1 The code that you provided is returning https://www.example.com/contact/page-1/index.html to https://www.example.com/contact/ Thanks – Ardian Jan 30 '19 at 15:29
  • Are you sure? In my environment it works as expected. – FedKad Jan 30 '19 at 15:57
  • Hi @Fedon Kadifeli Yes, that is correct. The redirect is going from https://www.example.com/contact/page-1/index.html to https://www.example.com/contact so it is skipping one directory. – Ardian Jan 30 '19 at 16:51
  • Can you paste the relevant part of the .htaccess file? (Also, check web server log to see that the redirection action is being taken and not the web browser cache is shown.) – FedKad Jan 30 '19 at 17:03
  • Hi @Fedon Kadifeli Only the code you have provided is in .htaccess RewriteEngine On RewriteBase / RewriteRule ^(.*)/index.html$ %{CONTEXT_PREFIX}/$1/ [R=301,L] Still the redirect is going from example.com/contact/page-1/index.html to example.com/contact so it is skipping one directory. Thank you! – Ardian Feb 01 '19 at 15:20
  • @ardian : Assuming that you have put your _.htaccess_ file under your `/contact` web folder and not on your _root_ web folder, you should replace the `RewriteRule` line with something like: `RewriteRule ^(.*)/index.html$ %{CONTEXT_PREFIX}/contact/$1/ [R=301,L]` – FedKad Feb 02 '19 at 12:56
  • @ardian : Please note that the above will not work for _example.com/contact/index.html_ though (because of the "/" preceding index.html in the matching side. For this, I suggest you add a second rule like: `RewriteRule ^index.html$ %{CONTEXT_PREFIX}/contact/ [R=301,L]` – FedKad Feb 02 '19 at 13:08