Redirecting index.html to root + folder index.html to root folder? (multilingual)

I have example.com page. I want a homepage without index.html (example.com/index.html -> example.com)

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

It works but what rule to create for folder /de/ (language)?

Code above in folder /de/ redirects me to example.com (example.com/de/index.html or example.com/de/index -> example.com). Correct link: example.com/de/

How to make a rule for /de/ folder? How to modify rule?

Expected effect

example.com/index.html example.com/index -> example.com
example.com/de/index.html example.com/de/index -> example.com/de/

.htaccess – example.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
ErrorDocument 404 /
Options +Multiviews

.htaccess – example.com/de/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
ErrorDocument 404 /de/
Options +Multiviews

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You basically just need to remove the /de/.htaccess file altogether!*1 Your root .htaccess already performs the necessary redirects.

(*1 Or remove all the directives and keep the ErrorDocument directive if you need a separate language-specific 404 file. But see the note below regarding the format of the ErrorDocument directive.)

The RewriteRule ^index\.html$ / [R=301,L] directive in the /de/.htaccess file is redirecting the request back to the document root and losing the subdirectory. But as stated above – this is not required anyway.

You will need to clear your browser cache, as 301s are cached persistently by the browser – better to test with 302 (temporary) redirects for this reason.

example.com/de/index

I would also question whether you really need to redirect index? This is only accessible because you’ve enabled MultiViews. Personally, I would disable MultiViews (which can cause additional conflicts with mod_rewrite if you’re not careful) and just let requests for /index fail with a 404.

With MultiViews enabled, all your HTML files and static resources are accessible without the extension.

In other words, to ensure that MultiViews is disabled:

Options -MultiViews

Note that MultiViews is disabled by default. (Although some shared hosts do explicitly enable it in the server config for some reason.)

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

You could reduce these two directives into one if you wish:

RewriteRule ^(|.+/)index\.html$ /$1 [R=301,L]
ErrorDocument 404 /

You should specify the full URL-path to the error document, instead of relying on mod_dir issuing a subrequest for the directory index. For example:

ErrorDocument 404 /index.html

Summary

Your root /.htaccess file:

# Disable MutliViews
Options -Multiviews

ErrorDocument 404 /index.html

RewriteEngine on

# Remove www subdomain and redirect to HTTPS
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Remove "index.html" from all URLs
RewriteRule ^(|.+/)index\.html$ /$1 [R=301,L]

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply