I'm trying to achieve a few things through .htaccess ,but keep running into issues. Before you tell me I need to research better and there's already a solution on this or a different forum, please know I've already done that. I always try and figure out things on my own before coming here, but this one is truly stumping me. Everything I've tried has only partially worked. Any help or education here would be truly appreciated.
My site has the following simple structure:
(root)
| index.html
| .htaccess
|
|___portal-folder
| index.php
| home.php
|
|_____admin-folder
| index.php
I'm looking to achieve the following:
When a user navigates to any base directory, for instance
site.comorsite.com/portal-folder/they don't see the index file nameindex.htmlorindex.phpin their browser.Same holds true if the user navigates to the full URL
site.com/index.htmlorsite.com/portal-folder/index.phpI would like the user to seesite.comorsite.com/portal-folder/respectively in their browser.Strip the file extension off all files in the browser. So for instance navigating to
site.com/portal-folder/home.phpwould show assite.com/portal-folder/homein the browser
The following code I'm using kind of works, but I'm getting strange behavior. For instance:
navigating to
site.com/portal-folder/indexdoesn't remove the index file name and show up assite.com/portal-folder/indexinstead ofsite.com/portal-folder/in the browsernavigating to
site.com/portal-folder/doesn't remove the index file name and shows up assite.com/portal-folder/index.phpin the browser.navigating to
site.com/portal-folder/index.phptakes the user back to the rootsite.comnavigating to
site.com/portal-folder/homeworks correctly, but navigating tosite.com/portal-folder/home.phpdoesn't strip the .php extension off.navigating to
site.comworks correctly, but navigating tosite.com/index.htmldoesn't remove the index file name.RewriteEngine On DirectoryIndex index.html index.php # remove trailing slash RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] RewriteRule ^(.+?)/$ /$1 [R=301,L] # To internally forward /dir/file to /dir/file.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.+?)/?$ /$1.php [L]
Server Information: Apache Version 2.4.46