HTACCESS Redirection using modrewrite

Login or register to post comments
3 replies [Last post]
profnotime's picture
Offline
Joined: 01/13/2009

Please can someone help me with a rule (or rules) that will redirect all incoming requests to my application to a particular page called index.php using the following format:

http://whatever.com/controller/action/parameter1/parameter2/parameter3/........../parameter N

Thanks in anticipation.

agsel's picture
Offline
Joined: 04/02/2008

I found following from google: http://stackoverflow.com/questions/484998/htaccess-at-any-length

I think you will never actually have unlimited number of parameters. You probably should go with the solution, where you use "controller", "action" and "params". Params will contain every parameter from 1 to N. You can parse it with php. I've used solution similar to it and works fine.

Offline
Joined: 05/15/2009

This is just a simple example although there are many different possible implementations:

in your .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?query=$1 [L,QSA]

in your index.php

require_once('includes/prepend.inc.php');
$query = '';
if (isset($_GET['query'])) $query = $_GET['query'];

// override your path info
QApplication::$PathInfo = $query;

// controller is in index 0, action in 1. parameters are 2 onwards
$strController = QApplication::PathInfo(0);
$strAction = QApplication::PathInfo(1);
switch($strController) {
case "page": include('bla bla bla'); break;
default: include('defaultpage.php'); break;
}

Then if you trigger something like http://www.yourdomain.com/page/view/2/contact.html
the controller will be 'page', action will be 'view', parameters will be 2 and contact.html.

Warning: be careful when you use built-in QCubed Form Drafts and stuff, they are using QApplication::PathInfo(0) by default to get the id parameter. You have to change it according your implementation rules.

Hope this helps.

profnotime's picture
Offline
Joined: 01/13/2009

Thanks so so so so so so much revvi, trying it out now and will get back to you if I run into any form of stress :D

I love QCUBED! Though its killing me here with loads and loads of OOP thinking :D....