问题原文
原帖链接 · masemo5547 · CC BY-SA 4.0
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
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
