Friendly URLs
From PRADO Wiki
This article assumes your server runs Apache or a similar http daemon that also supports mod_rewrite directives in .htaccess files.
The .htaccess files here assume a few things:
- Service names are all lowercase letters
- Page names are all CamelCase letters
Liberalize the regular expressions as necessary.
A new HttpRequest object
Create a new HttpRequest object that only overrides the constructUrl function. Make the indicated changes.
<?php
class HttpRequest extends THttpRequest
{
public function constructUrl($serviceID,$serviceParam,$getItems=null,$encodeAmpersand=false,$encodeGetItems=true)
{
//$url=$serviceID.'='.$serviceParam;
...
if($this->getUrlFormat()==='Path')
{
...
} else {
...
//return $this->getApplicationUrl().'?'.$url;
$base_url = str_replace('index.php', '', $this->getApplicationUrl());
$base_url .= $serviceID . '/' . $serviceParam;
if (!empty($url))
{
$base_url .= '?' . $url;
}
return $base_url;
}
}
}
?>
Setting up the .htaccess file
Create a .htaccess file in a web-accessible directory at or above the level of the PRADO project's installation. Make sure the file is readable by the Apache process (or whatever user will access it in the case of suexec).
application.xml
<application id="..." mode="..."> <paths>
<using namespace="Application.pathTo.HttpRequest" />
</paths> <modules> <module id="request" class="HttpRequest" UrlFormat="Get" /> ... </modules> ... </application>
.htaccess
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# /service/Path.toPage format
RewriteRule ^([a-z]*)/([A-Za-z\.]*)/{0,1}$ index.php?$1=$2 [QSA,L]
# /service/Path.toPage?getvars format
RewriteRule ^([a-z]*)/([A-Za-z\.]*)/{0,1}\?(.*)$ index.php?$1=$2&$3 [QSA,L]
# /[anything]/service/Path.toPage format
RewriteRule ^(.*)/([a-z]*)/([A-Za-z\.]*)/{0,1}$ index.php?$2=$3 [QSA,L]
# /[anything]/service/Path.toPage?getvars format
RewriteRule ^(.*)/([a-z]*)/([A-Za-z\.]*)/{0,1}\?(.*)$ index.php?$2=$3&$4 [QSA,L]
What this does
- Any file that really exists won't be subject to the rewrite rule. So, don't worry about the assets, theme, and other folders.
- URLs are treated as [/anything]/service/request[?get_vars], where parts in brackets are optional. For example, valid page requests would be http://example.com/page/Path.toPage and http://example.com/subdir/page/Path.toPage.
Even shorter URLs
This removes the "page" part from the path and just assumes it. This is probably fine for most sites, as they only use the "page" service. You will need to modify the HttpRequest object to return URLs that fit this scheme because the example object above is designed for the other mod_rewrite scheme.
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# /Path.toPage format
RewriteRule ^([A-Za-z\.]*)/{0,1}$ index.php?page=$1 [QSA,L]
# /Path.toPage?getvars format
RewriteRule ^([A-Za-z\.]*)/{0,1}\?(.*)$ index.php?page=$1&$2 [QSA,L]
# /[anything]/Path.toPage format
RewriteRule ^(.*)/([A-Za-z\.]*)/{0,1}$ index.php?page=$2 [QSA,L]
# /[anything]/Path.toPage?getvars format
RewriteRule ^(.*)/([A-Za-z\.]*)/{0,1}\?(.*)$ index.php?page=$2&$3 [QSA,L]

