Reputation: 22233
I would like to translate a url with parameters to a path, i.e.
http://www.example.com/?page=1 => http://www.example.com/1
http://www.example.com/?section=books&category=thriller&page=1 => http://www.example.com/books/thriller/1
And then i would like to be able to get all this parameters to use them in a query in order to make a select based on this parameters, i don't know where to start, how can i do it? I'm using this .htaccess to redirect all my requests to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
How can i "translate" a path to a set of parameters to use them in a query?
Upvotes: 1
Views: 824
Reputation: 54
I would have done something like this ...
RewriteRule ^([0-9]+)$ index.php?page=$1
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9]+)$ index.php?section=$1&category=$2&page=$3
Upvotes: 0
Reputation: 32232
You have it backwards. You want to have the request come in from the user as http://www.example.com/books/thriller/1
, then rewrite that to index.php?qs=$1
for PHP to handle. This way the user sees the 'friendly' URLs, and PHP sees the format that is friendly to it.
So your rewrite rule would change to something like:
RewriteRule ^(.*)$ index.php?qs=$1 [QSA,L]
And then you can access its contents via:
$myQueryVars = explode('/', $_GET['qs']);
http://www.example.com/books/thriller/1
becomes
array( 'books', 'thriller', '1' )
Upvotes: 1