Link to home
Start Free TrialLog in
Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

asked on

How to use .htaccess to hide/remove .php from all files (including those inside folders)

Hello Experts!

I need help on how to use .htaccess to hide/remove .php from all my files and those inside folders.

I've tried to add .htaccess to the root of my folder, I could only access index.php redirected to login.php (showing login) but I couldn't move pass that page.

I was getting Page Not Found giving me wrong url.

Here is the code I've used inside .htaccess:
RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Open in new window

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

Open in new window

Separately, both yielded same result.

Am I supposed to indicate all folders name in the .htaccess? and how?

Someone even suggested I should put .htaccess inside all folders which I tried but I couldn't login (invalid login details). After removing them (.htaccess), I was able to login again.

Please help
Avatar of David S.
David S.
Flag of United States of America image

Did you change the action of your login form so that the redirect isn't needed?

REQUEST_FILENAME might not work as you might expect, so try using REQUEST_URI instead. Also it looks like you don't want to remove the query string, so add the QSA option to include it in the redirects.
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/]+)/$ https://mysite.com/$1 [R=301,QSA,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://mysite.com/$1 [R=301,QSA,L]

Open in new window

Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

ASKER

I tried your suggestion, I got same. In the address bar:
https://mysite.com/controllers/auth

Open in new window

On the page:
Not Found
The requested URL /controllers/auth was not found on this server.


Note: controllers is a folder in which auth.php is located. auth.php is a script that controls login and redirects to dashboard. I believe the path is correct but I don't know why I'm still getting NOT FOUND.

AM I supposed to remove all .php extensions in all files? As in <a href="admin/dashboard.php"> ?

Did you change the action of your login form so that the redirect isn't needed?
I didn't. It has always been like this:
switch ($user->Role):

            case 'Admin':
                $response->message = "admin/dashboard.php";
                break;

            case 'Form Master':
                $response->message = "form_masters/dashboard.php";
                break;

            case 'Subject Teacher':
                $response->message = "staff/dashboard.php";
                break;

            case 'STU':
                $response->message = "students/index.php";
                break;

        endswitch;

Open in new window

Perhaps mod_negotiation is interfering. Try adding this to your .htaccess file:
Options -MultiViews

Open in new window

Not working. Anything else I need to try?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.