Diego Santamarta
Diego Santamarta

Reputation: 45

Subfolder/Subdirectory and clean URL's

after doing tons of tests and looking a lot into stackoverflow, I can't figure out how to achieve this.

I have a website served in a subfolder (www.domain.com/subfolder/). I have the .htaccess located in that subfolder (www.domain.com/subfolder/.htaccess) and I have 3 htmls (index.html, example1.html, example2.html)

I want when you access to www.domain.com/subfolder/example2.html it opens www.domain.com/subfolder/example2/ with clean urls and with css, js and image paths working.

I don't know how to set this subfolder as root and continue working clean url's.

This is mi actual .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

I've tried several thing but seems nothing works.

Thanks in advance. Diego

Upvotes: 1

Views: 1443

Answers (1)

anubhava
anubhava

Reputation: 784898

Try this in /subfolder/.htaccess:

RewriteEngine On
RewriteBase /subfolder/

# To externally redirect /subfolder/file.html to /subfolder/file/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

## To internally redirect /subfolder/file/ to /subfolder/file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

For css,js,image etc make sure to use absolute paths in your HTML or else you can add this just below <head> section of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.

Upvotes: 1

Related Questions