Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess routing PHP

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?

like image 524
Bob Avatar asked Mar 01 '14 07:03

Bob


People also ask

What is htaccess in PHP?

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 .

What is PHP routing?

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.

What is routing please explain using an example in PHP?

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.


1 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.

like image 87
Rápli András Avatar answered Oct 05 '22 16:10

Rápli András