I have presently got an internet application which i need optimizing, and something of techniques or something like that I am attempting to achieve is really:




I have presently this because the PHP code for this:

if(empty($_REQUEST['dept'])) {     
 $folder = "apps/"; 
} else {    
 $folder = str_replace("/", "", $_REQUEST['dept']) . "/"; }    
  if(empty($_REQUEST['n'])) {     
	 } else {     
	       $app = $_REQUEST['n'];
	      if(file_exists($folder.$app.".php")) {          
			 } else  {  				 

How do you do that?

I am presently half there with:

RewriteRule ^([A-Za-z]+)$ /index.php?app=$1

but that only rewrites some of it.


The way in which many frameworks do that is and among the next rules:

RewriteRule ^(.*)$ /index.php?q=$1
RewriteRule ^(.*)$ /index.php

Within the first situation you receive the query string in $_GET["q"].
Within the second situation you need to obtain the query string from $_REQUEST or something like that. (simply do some var_dumps until you find the thing you need).
Then you definitely explode("/") this and you are ready.

Take a look at how TYPO3, eZPublish, Drupal do that.

It's also wise to add the next conditions to permit the website to spread out your static files (like images/css/js/etc). They tell apache not to perform the rewrite when the URL points to some location that really matches personal files, directoy or symlink. (You have to do this prior to the RewriteRule directive)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

This will work:

RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?dept=$1&app=$2 [QSA]

You'll need the QSA part to ensure that any GET parameters to become appended towards the rewritten URL.

You will probably find that it may become more flexible to rewrite everything to index.php, after which handle separating the url there, e.g.


#only rewrite paths that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1


$parts = explode('/', $_SERVER['PATH_INFO']);
$dept = isset($parts[0]) ? $parts[0] : 'someDefault';
$app = isset($parts[1]) ? $parts[1] : 'anotherDefault';