Home » Php » apache – .htaccess routing PHP

apache – .htaccess routing PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to write some simple routing in htaccess for PHP

My file looks like this for now:

RewriteEngine On
RewriteBase /webservices/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ server.php [QSA,L]

But it does nothing.

The thing that I want with htaccess is that all the calls to server.php are caught like this:

http://localhost:8888/webservices/server.php?username=test&password=test

to be written

http://localhost:8888/webservices/server/u/test/p/test/

Can someone help me with that?

How to&Answers:

It is actually working, because it redirects all queries to server.php. Therefore

http://localhost:8888/webservices/server/u/test/p/test/

can be handled by server.php, where you can easily catch parts of the URI using explode() on the $_SERVER["REQUEST_URI"] array. Forget the

http://localhost:8888/webservices/server.php?username=test&password=test

URL structure, you won’t need it anymore, because using the .htaccess you’ve just written, you can dynamically handle every request to your server on server.php.

[QSA] in your .htaccess appends query string to the URI, which is not needed, because we want to get rid of it, right? Since you wont use that format, you can remove QSA flag.