RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /fr/index.html [L]
Aside: If you removed the RewriteBase
directive and the /fr/
prefix on the RewriteRule
substitution then all your .htaccess
files in the locale subdirectories would be the same.
However, I would go for creating a single .htaccess
file in the document root (parent directory) instead, as you suggest.
In order to remove the trailing slash from the language subdirectory you do need the DirectorySlash Off
directive - in the root .htaccess
file. However, you also need to manually rewrite the request to append the slash in order to "fix" the URL. (mod_dir etc. won't work correctly without the trailing slash.)
Try something like the following in the root .htacces
file, instead of multiple .htaccess
files in subdirectories:
# Prevent mod_dir from appending the trailing slash to directories (with a 301 redirect)
DirectorySlash Off
DirectoryIndex index.html
RewriteEngine On
# Allow mod_rewrite to match directories that do not have a trailing slash (when DirecrorySlash Off)
RewriteOptions AllowNoSlash
# Rewrite directory without trailing slash directly to the front-controller
RewriteRule ^([a-z]{2})$ $1/index.html [L]
# Front-controller to respective language controller
RewriteRule ^[a-z]{2}/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/. $1/index.html [L]
Currently this does not do anything with requests for the document root, or "files" therein.
Also, any request for a language code that simply looks-like a language code is routed to that fron-controller, whether it exists or not (404). eg. /ab/contact
is routed to /ab/index.html
.
A request for /fr/
(with a trailing slash) - if the user should request it - is handled by mod_dir in the usual way and will naturally issue an internal subrequest for index.html
(if the directory exists) - the DirectoryIndex
. This could be externally redirected to remove the trailing slash, although you should already be linking to the slash-less URL and the appropriate canonical URL set - so it shouldn't be an SEO issue (unless the slashed URL is indexed/linked to in the wild).
Note that by disabling the DirectorySlash
, this is then disabled for all child directories as well, unless you explicitly override this.