John Davidson

php - .htaccess rewrite GET variables

0 comments
Message:


I have a index.php which handle all the routing index.php?page=controller (simplified) just to split up the logic with the view.



Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]


Which basically:
http://localhost/index.php?page=controller
To



http://localhost/controller/



Can anyone help me add the Rewrite for



http://localhost/controller/param/value/param/value (And soforth)



That would be:



http://localhost/controller/?param=value&param=value



I can't get it to work with the Rewriterule.



A controller could look like this:



    <?php
if (isset($_GET['action'])) {
if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>


And also:



    <?php
if (isset($_GET['action']) && isset($_GET['x'])) {
if ($_GET['action'] == 'delete') {
do_Delete_stuff_here();
}
}
?>


My Answer:
You can achieve this by adding the following RewriteRule to your .htaccess file:


RewriteRule ^([\w\d~%.:_\-]+)/([\w\d~%.:_\-]+)/([\w\d~%.:_\-]+)/([\w\d~%.:_\-]+)/?$ index.php?page=$1&$2=$3&$4=$5 [NC]


This rule will rewrite URLs in the format http://localhost/controller/param/value/param/value to http://localhost/controller/?param=value¶m=value. You can continue adding additional parameters as needed by adding more pairs of param/value in the rule.

Rate this post

5 of 5 based on 5067 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us