Mobile app version of vmapp.org
Login or Join
Kevin317

: 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,

@Kevin317

Posted in: #Htaccess #ModRewrite

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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kevin317

1 Comments

Sorted by latest first Latest Oldest Best

 

@XinRu657

mod_dir appears to be thwarting your efforts. Give this a try instead:

DirectorySlash off

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule .* %{REQUEST_FILENAME}.html [L,QSA]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme