2

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
5
  • Your rule now looks correct to me. You could have an old redirect cached by your browser. Have you tried clearing your browser cache before testing again? Have you tried testing with curl --head on the command line? Nov 17, 2019 at 11:28
  • To confirm... you only have this code in the .htaccess file in the document root directory? You have no other .htaccess file? Do you have any other directives in your .htaccess file?
    – MrWhite
    Nov 17, 2019 at 11:44
  • Cache clean - not working. Question edited @MrWhite
    – masemo5547
    Nov 17, 2019 at 13:26
  • "I want a homepage without index.html" - To confirm, you are already linking to the canonical URLs, without index.html?
    – MrWhite
    Nov 17, 2019 at 22:23
  • 1
    Yes, canonical URLs without .html
    – masemo5547
    Nov 17, 2019 at 23:11

1 Answer 1

2

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]
1
  • 1
    Thank you, everything works now
    – masemo5547
    Nov 17, 2019 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.