-1


When a URL is typed with a slash at the end it returns 404 not found page.
For example: http://www.example.com/index/ gives 404 not found.
what code to add in .htaccess to redirect it?
For directories, it works fine but for html files it doesnt.

NOTE: I have included a code in .htaccess to remove file extensions so index.html becomes html and so on.

MEGA
  • 261
  • 1
  • 2
  • 9
  • NOTE: Now you must also add this code in the question! – Croises Aug 26 '17 at 18:37
  • @Croises the code to hide file extensions is not relevant to the question. I have said that to make it clear that `index` in the example provided is a file and not a directory "folder". – MEGA Aug 26 '17 at 18:44

1 Answers1

1

You can just simply remove all trailing slashes from your URLs using:

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

Just make sure you clear your cache before testing this.

Joe
  • 4,877
  • 5
  • 30
  • 51
  • will this be applied to directories like `/images'? which I don't want. – MEGA Aug 26 '17 at 19:17
  • It will be, but you can stop that form happening by using `RewriteCond %{REQUEST_URI} !^/images` – Joe Aug 26 '17 at 19:20