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.com
orsite.com/portal-folder/
they don't see the index file nameindex.html
orindex.php
in their browser.Same holds true if the user navigates to the full URL
site.com/index.html
orsite.com/portal-folder/index.php
I would like the user to seesite.com
orsite.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.php
would show assite.com/portal-folder/home
in 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/index
doesn't remove the index file name and show up assite.com/portal-folder/index
instead 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.php
in the browser.navigating to
site.com/portal-folder/index.php
takes the user back to the rootsite.com
navigating to
site.com/portal-folder/home
works correctly, but navigating tosite.com/portal-folder/home.php
doesn't strip the .php extension off.navigating to
site.com
works correctly, but navigating tosite.com/index.html
doesn'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