Search code examples
html.htaccessdirectorycpanel

How do you make a subpage of a subpage for a website using only cpanel that hides the .html file extension?


I have a website with subpages that have URLs which look something like this:

https://www.example.com/hello/world
https://www.example.com/hello/earth

In cpanel, my file structure for those pages looks something like this:

[folder]
 hello
 └ [folder]
    world.html
    earth.html
 .htaccess
 index.html

My .htaccess file has the following rule to account for missing .html file extensions:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

How do I make https://www.example.com/hello load as a working webpage? I have tried simply adding hello.html to the folder to no avail -- like so:

[folder]
 hello
 └ [folder]
    world.html
    earth.html
 .htaccess
 hello.html
 index.html

When I attempt to access https://www.example.com/hello in that example, it leads me to my 404 page, but .../hello/world and .../hello/earth still work. https://www.example.com/hello.html does work, but I don't want the .html file extension to be visible to the end user.

I have yet to find a solution to hide the .html file extension in this situation. Help in doing so would be appreciated!


Solution

  • When I attempt to access https://www.example.com/hello in that example, it leads me to my 404 page

    Because you have a directory of the same name and (by default) mod_dir will issue a 301 redirect to fix/append the trailing slash. The 404 then results from the redirected request when your directive attempts to append the .html file extension by rewriting the request (now with a trailing slash) from /hello/ to /hello/.html (which naturally results in a 404).

    To prevent mod_dir appending a trailing slash to requests for directories you can include the following directive at the top of the file:

    # Prevent mod_dir appending a trailing to directory requests
    DirectorySlash Off
    
    # Disable auto-generated directory listings (mod_autoindex)
    Options -Indexes
    

    You will need to make sure your browser cache is cleared before testing since the earlier 301 (permanent) redirect (by mod_dir) will have been persistently cached by the browser.

    For security, you also need to ensure that auto-generated directory listings are disabled (unless you explicitly want this behaviour), since when DirectoryIndex Off is set and you request a directory without a trailing slash, mod_autoindex will still generate a directory listing even if a Directoryindex document is present in that directory.


    Alternative solution

    Alternatively, you can still allow mod_dir to append the trailing slash (so a request for /hello is still redirected to /hello/ and so /hello/ is the canonical URL), but allow for an optional trailing slash in the RewriteRule pattern, but exclude this from the capturing subpattern by using a non-greedy regex.

    For example:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+?)/?$ $1.html [L]
    

    With a request for /hello/ then hello is captured by the parenthesised subpattern, so it is rewritten to hello.html as above.