1

I want to hide all file extensions from the URL with .htaccess without defining each extension in the .htaccess. I have found a few options out there using mod_rewrite and mod_negotiation, but either they are incomplete or not explained well. I'd prefer not to simply copy and paste but also understand.

Here are a list of desired results:

  • Removal of all file extensions for the URL when visiting, be that html, php, css, jpg, etc... We do not want to have to define each one in .htaccess.

  • Have all files still load without their extensions

  • If the index of a directory or only file in the directly, root or otherwise, simply display the folder for example example.tld/onlyfile.ext -> example.tld or example.tld/directory/onlyfile.ext -> example.tld/directory/

  • When navigating to a file, add a trailing slash for example example.tld/somefile.ext -> example.tld/somefile/

If this is not possible it would be nice to hear that rather than just another repeat of how to redirect each individual extension.

Thanks for any help!

  • This is possible, but not purely by request rewriting. Reason is that your server cannot somehow magically _guess_ what file actually is requested. So you have to implement some form of order in which possibly meant extensions are probed for existence. That however is an implementation of all possible "file name extensions" you want to handle. What you _can_ do instead is simply remove the extension from the actual files. You may have to help the http server to send the correct mime types then however which is best dome using a router script or similar. – arkascha Mar 26 '23 at 11:42
  • I am not entirely sure why you assume the server would be guessing what file is requested? Are you addressing a specific point? The request being made by the website (through a link or something) will obviously still point to the correct file, it simply won't display that way to the user. No guess work, unless I don't understand... – Snape Ninja Mar 26 '23 at 21:46
  • Maybe I am missing something, but I fail to see how the server should "know" which file exactly is requested. Usually that is done by looking for the exact path/filename taken from the requested URL. If it is present in the requested folder, then the file gets delivered. That is not possible with only a part of the file name, obviously. So how should the server "know" which file has actually been requested? Or asked in a different way: is it _really_ a specific file that has been requested? I doubt that. Consider two files with the same base name and different extensions in a folder... – arkascha Mar 27 '23 at 06:01
  • Yes I think you are, this is just about hiding the file extension from the user in the URL. The server will know what it will be serving because on the server itself it will have the correct extension. If someone requested the URL without the extension, the server would still know because there will always only be one file with that name. Additionally mod_negotiation helps with that. – Snape Ninja Mar 27 '23 at 06:41
  • No, that clearly is wrong. Even if there is only one file with that base name, the server still cannot somehow "magically" know which file to try. You'd have to _try_ all possible extensions, which in turn means you need to name all possible extensions. Just as I mentioned initially. I think you should invest a bit into understanding how request rewriting actually works. It is purely string based, there is no "knowledge" in the server about URLs. – arkascha Mar 27 '23 at 06:55
  • Here is an example of what I am talking about: https://stackoverflow.com/questions/27156944/how-to-remove-multiple-file-extensions-with-htaccess-and-redirect-file-extension You can see it does work in removing the extension and will load the correct page even if you request it without the extension or without the file name. However it is incomplete and does not fulfil all the asks I have such as removing the name outright automatically or adding the trailing forward slash. – Snape Ninja Mar 27 '23 at 08:12
  • `MultiViews` is an option that causes more trouble than it saves. I strongly advise against using it. You will find many examples here on SO where that option is exactly the cause of issues. It makes the rewriting process (and other aspects) hard to debug, it is hard to predict what resource the system actually will pick for a request - not a situation you want. But it is your decision. – arkascha Mar 27 '23 at 12:48

0 Answers0