1

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

1 Answers1

1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Anubhawa ,I am curious to knw, why do we need to match the slash /+ 1 or more times in THE_REQUEST? – Amit Verma Dec 10 '15 at 13:32
  • Hi Anubhava, Have tryed it and doesn't work. When i acces to ``www.domain.com/subfolder/``it look for css, images and js in ``www.domain.com``and when I try to access to ``www.domain.com/subfolder/example2.html``it goes to ``www.domain.com/example2/`` instead of going to ``www.domain.com/subfolder/example2/`` – Diego Santamarta Dec 10 '15 at 13:34
  • 1
    @Starkeen: some one can theoretically enter `http://domain.com////subfolder/file.html` as URL as well – anubhava Dec 10 '15 at 13:40
  • @anubhava you're right that works. The issue now is with css, images and js. And if I'm on ``localhost/subfolder/example2/ `` and I click the link to ´´example1.html´´ it goes to ``localhost/subfolder/example2/example1/`` – Diego Santamarta Dec 10 '15 at 13:52
  • The correct path is in ``localhost/subfolder/css`` and ``localhost/subfolder/js`` – Diego Santamarta Dec 10 '15 at 13:58
  • @anubhava ¿There's no option to do this without using the base?, and if I'm on localhost/subfolder/example2/ and I click the link to ´´example1.html´´ it goes to localhost/subfolder/example2/example1/ – Diego Santamarta Dec 10 '15 at 14:04
  • Thanks @anubhava but I have my css like this ```` and still doesn't work – Diego Santamarta Dec 10 '15 at 14:21
  • As you wrote earlier that `The correct path is in localhost/subfolder/css` NOT `localhost//css` that's why `` will give you 404 – anubhava Dec 10 '15 at 14:40
  • @anubhava Last doubt, you can't set the subdirectory as root to avoid this? – Diego Santamarta Dec 11 '15 at 08:39
  • 1
    If you have access to Apache server yes then you sure can – anubhava Dec 11 '15 at 08:52
  • I have access. How can I do this? – Diego Santamarta Dec 11 '15 at 10:08
  • That can't be answered from comments. I suggest creating a new question so that you get better help. – anubhava Dec 11 '15 at 14:28