Rewrite query string "?x=y" style to "/x/y" style
I have a PHP MVC framework I've built from scratch which uses the traditional domain.com/controller/action
URL routing. While I'm currently handling the below conversion in the router I'd like to replace them in the URL for cosmetic reasons.
For example:
controller/action?filter=bank
Becomes:
controller/action/filter/bank
I've done a bit of experimentation with a regex but can't se开发者_Go百科em to find a match. I'm also not sure how to rewrite it using RewriteCond
.
Thanks in advance.
You should take a look at some of the questions on clean-urls, mod-rewrite and .htaccess.
HTAccess - Confusing clean url's
Getting two variables in the URL with htaccess
Needs some help with an apache mod-rewrite issue
The Zend Framework URL format is nearly identical to this, and here's what they use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
If your server-side script is PHP, you could use this to grab the url parts:
$request_parts = explode('/', $_SERVER['REQUEST_URI']);
$controller = $request_parts[0];
$action = $request_parts[1];
// etc...
Based on your response to my other answer, I propose that you use JavaScript to handle the filter form submit and create the "clean url" request on the client side.
EDIT
I recently implemented this on my Year/Month picker for my calendar application. It uses jQuery/JavaScript to detect changes in the form and write the "clean" URL:
$(document).ready(function() {
$('form.jumpform select').change(function() {
$form = $(this).closest('form');
window.location = $form.attr('action')
+ '/y/' + encodeURIComponent($form.find('select[name="y"]').val())
+ '/m/' + encodeURIComponent($form.find('select[name="m"]').val());
});
});
精彩评论