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
or site.com/portal-folder/
they don't see the index file name index.html
or index.php
in their browser.
Same holds true if the user navigates to the full URL site.com/index.html
or site.com/portal-folder/index.php
I would like the user to see site.com
or site.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 as site.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 as site.com/portal-folder/index
instead of site.com/portal-folder/
in the browser
navigating to site.com/portal-folder/
doesn't remove the index file name and shows up as site.com/portal-folder/index.php
in the browser.
navigating to site.com/portal-folder/index.php
takes the user back to the root site.com
navigating to site.com/portal-folder/home
works correctly, but navigating to site.com/portal-folder/home.php
doesn't strip the .php extension off.
navigating to site.com
works correctly, but navigating to site.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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…