Mobile app version of vmapp.org
Login or Join
Kaufman445

: .htaccess mod rewrite sub directories redirect loop 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

@Kaufman445

Posted in: #Apache2 #Htaccess #ModRewrite

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 (.*)/$ .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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kaufman445

1 Comments

Sorted by latest first Latest Oldest Best

 

@Harper822

The issue was primarily due to the fact that the rule was not specific to .html files. This meant that all files would get redirected including .css files.

Here is a working .htaccess file:

<IfModule mod_rewrite.c>
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /sub

# redirect urls without trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*)$ / [L,R=301]

# remap url to a .html file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/$ .html [L]

</IfModule>


Relative paths within the html file still had to be changed to absolute paths like /sub/test.css

Non-existing URL's (no matching file) still end up with redirect loops which is not ideal.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme