N. Johnson
N. Johnson

Reputation: 101

Redirect folder to html file

I'm trying to set up a website that was once setup on wordpress but is now flat-file. The flat files are all .html files in the root folder, and I need to do an htaccess rewrite so that urls like: mysite.com/test and mysite.com/test/ BOTH are treated the same, but also serve an .html file that matches the url (for example, mysite.com/test.html is served when mysite.com/test, mysite.com/test/, mysite.com/test.html and so on are requested).

Here's the code I'm trying which gets me everything except when a user requests mysite.com/test/ (with a trailing slash).

Desired result: mysite.com/test, mysite.com/test/, mysite.com/test.html ALL serve test.html (but the url is usually written without the html extension)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

Upvotes: 0

Views: 615

Answers (2)

N. Johnson
N. Johnson

Reputation: 101

I added:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

Below the rewrite engine on line and this fixed my problem along with the other answer here.

Upvotes: 0

AbsoluteƵER&#216;
AbsoluteƵER&#216;

Reputation: 7880

Look for anything that's not a slash.

RewriteRule ^([^/]+)$ $1.html [L,QSA]

Upvotes: 0

Related Questions