Search code examples
.htaccessdirectoryindex

.htaccess DirectoryIndex page links broken


In my .htaccess file the DirectoryIndex is set to /page/main/home.html

Therefore when I go to www.thisexample.com the browser address bar shows www.thisexample.com while actually loading www.thisexample.com/page/main/home.html. This is all great but now onto the problem:

Any .html pages that are linked to from home.html can't be found. In other words when I click on the info link (on the home page) it looks for www.thisexample.com/info.html (non-existant) instead of showing www.thisexample.com/info.html in the browser address bar but actually loading www.thisexample.com/page/main/info.html (the ultimate goal here).

It might help to add that home.html has no issue finding it's css stylesheet through a relative path, just can't find the links.


Solution

  • DirectoryIndex is supposed to be the name of default file name like index.html or index.php for ex:

    DirectoryIndex index.html
    

    If you want to load all the .html files from /page/main then have these rewrite rules:

    DirectoryIndex home.html
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
    RewriteRule !^page/main/ page/main%{REQUEST_URI} [L,NC]
    

    Also start using absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.