: Apache: use `foo.html` in preference to `foo/` when both exist How to get Apache 2.2 to use foo.html in preference to foo/ when both exist? I'm moving a site from dynamic php to static html,
How to get Apache 2.2 to use foo.html in preference to foo/ when both exist?
I'm moving a site from dynamic php to static html, while retaining the domain name. I've created a temporary mirror using httrack. The mirror site works well, except that pages which used to be foo/about are now foo/about.html. When I replace the live site with the mirror all incoming links and bookmarks will be broken, something I want to avoid.
I've discovered using Options +MultiViews in my .htaccess file achieves most of the above, except when file and directory names collide: if the url is example.org/how_to apache tries to load how_to/index.* instead of how_to.html when the latter exists.
I've tried a few mod_rewrite rule sets but only succeeded in duplicating the multiviews behaviour at the expense of a more complicated .htaccess file:
# Add .html internally (but remove externally):
RewriteEngine on
RewriteBase /
# Skip the next 2 rules (the .html stripping redirections)
# if the current URL doesn't end with .html
# or the requested file doesn't exist:
RewriteCond %{THE_REQUEST} !.html [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .? - [S=2]
# Redirect /dir/index.html to /dir/:
RewriteRule ^(.*/|)index.html$ [R=301,L]
# Redirect /dir/file.html to /dir/file:
RewriteRule ^(.+).html$ [R=301,L]
# Internally, treat request for /dir/file as /dir/file.html
# (but only if /dir/file.html exists):
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ .html [L]
As you've likely surmised by now. I'm on a shared host and can only use .htaccess directives. This is static html, php, cgi, etc. are not available.
Hat tip to cjm and pne on the nearlyfreespeech.net forum who got me this far.
More posts by @Kevin317
1 Comments
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.