Skip to content
Advertisement

How to remove .php and replace ? with / in htaccess

How do I remove .php and replace ? with / at the same time.

I have

http://localhost/testingproject/testing.php?mode=getdata

I want it to be called from the browser like this:

http://localhost/testingproject/testing/getdata

In replacing ? data, I want it to be compatible with dir.

Like in dir, I have:

testing.php
user.php
dashboard.php

So, if the user calls http://localhost/testingproject/user, alone, there is no need to parse ? to / as user is a valid PHP file: user.php.

But if user the calls http://localhost/testingproject/user/abc, then user.php page should get $_GET['mode'] value abc.

I’ve searched but still cannot find compatible htaccess code.

Advertisement

Answer

I found one that works. Now, i can call http://localhost/testproject/backend/user/abc instead of http://localhost/testproject/backend.php?mode=abc In Replace GET variables with slashes .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?mode=$2 [L,QSA]

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement