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?
htaccess (Hypertext Access) file is an Apache distributed server configuration file. You can use the . htaccess file to set server configurations for a specific directory. This directory can be the root directory of your website or another subdirectory where the .
Routing is what happens when an application determines which controllers and actions are executed based on the URL requested. Simply put, it is how the framework gets from http://localhost/users/list.html to the Users controller and the list() action.
In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test. php in a pre-defined directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With