Reputation: 705
I'm attempting to use mod_rewrite with Apache 2.4 to append the suffix .html
to request URIs.
The URIs that should be rewritten are very simple and take the following form:
http://host.domain/page/
The above needs to be rewritten to http://host.domain/page.html
. Only constraint is that the rewriting logic must ignore URIs referencing actual files or directories.
So far the rewriting snippet I came up with works fine if there is no trailing slash, however if one is present Apache emits a 404 and the following error message:
The requested URL /redirect:/about.html was not found on this server.
(the above happens when the URI is http://localhost/about/
)
Could someone help me debug this? Why is Apache prepending /redirect:
?
Here's a very simple snippet that reproduces the symptoms:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+[^/])/$ /$1 [C]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^$
RewriteRule (.*) /$1.html [L,R=301] # Tried without R=301 too
# This doesn't work either.
# RewriteRule ^about/$ /about.html [L,R=301]
Upvotes: 1
Views: 1801
Reputation: 785128
You can use:
RewriteEngine on
RewriteBase /
# strip trailing slash from non-directoies
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [L,R=301]
# make sure corresponding html file exists
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Upvotes: 3