I have only access via
.htaccess. So I bet I have to remove these "<IfModule mod_rewrite>" and "</IfModule>"?
I tried the following:
- Code: Select all
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
And -- wow -- it works! This is great, thank you
Additions to my requirements:
/index.html becomes
/index. And it would be cool, if this "index" could be stripped off, too (because it's only the name of the file Apache looks for in a folder at first, it offers no benefit to the user)
Questionable:
if
/foobar (a file without any file extension) is lying in the same folder as
/foobar.html, this htaccess code would get the HTML version, if you call
/foobar. Now, the problem is, that you can never get the file
/foobar. Might there be a solution for this? I think, I'll never upload files without file extension, but who knows ... it's not so important, though.
I try to understand, what the code does:
Please correct me!
DirectorySlash Off:
I
read about it (don't understand all the English, though). As far as I understand, it disables the automatic addition of the trailing slash, when a name is called, that matches a folder name.
But I wonder ... it still does add a trailing slash, hm ...
SCRIPT_FILENAME
= the name of the requested file
REQUEST_URI
= SCRIPT_FILENAME + potential query strings
----------------------------------------------------------------------------------------
RewriteCond %{SCRIPT_FILENAME}/ -d:
checks, if [the requested file + a trailing slash]
IS a folder (d: directory).
If it's not, stop here?
RewriteCond %{SCRIPT_FILENAME}.html !-f:
checks, if [the requested file + the extention .html]
IS NOT a file (f: file).
If it is a file, stop here?
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]:
if the two conditions are true, this RewriteRule gets executed:
checks, if the entered URL does NOT end with a trailing slash. If it's true, it adds a trailing slash to the URL (redirect 301) and stops here (L).
----------------------------------------------------------------------------------------
RewriteCond %{ENV:REDIRECT_STATUS} ^$:
I'm not sure. Does it mean: "if nothing got redirected until now" (so: if the conditions of the first block were not true)?
RewriteRule ^(.+)\.html$ /$1 [R=301,L]:
I don't get it
----------------------------------------------------------------------------------------
RewriteCond %{SCRIPT_FILENAME}.html -f:
checks, if [the requested file + .html] IS a file.
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
I don't get it