I am trying to setup a sub-directory with individual .htaccess. I want to redirect .html files to url without extensions and also add a trailing slash and redirect all non-slashed urls there.
Here's my the .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /test
# redirect urls without trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*)$ %{REQUEST_URI}/ [L,R=301]
# remove .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/$ $1.html
</IfModule>
It does work but relative paths get .html added again and again until the max redirect limit. Sample paths that are affected: /css/test.css and css/test.css. /sub/css/test.css works fine.
Changing RewriteCond %{REQUEST_FILENAME}
!-f to RewriteCond %{REQUEST_FILENAME}.html !-f
or a similar permutation doesn't seem to have any effect at all.
I only want to redirect .html files. What am I missing here?
RewriteRule
only applies to requests that are not directories or files. (?) How do you have/css/test.css
and/sub/css/test.css
- I would have thought only one of those exists (the one that "works")? Your directives only apply to files that don't physically exist. Incidentally if you did a check for_FILENAME}.html !-f
then you would need to remove the!
- you are checking that the file exists, not that it deosn't exist.