URL Amigável php e .htacces
Bom pessoal seguinte tneho o meu código LoddAlteraUrl.class.php:
<?php
class CodonRewrite
{
public static $rewrite_rules = array();
public static $get;
public static $controller;
public static $current_module;
public static $current_action;
public static $params;
public static $peices;
public static $run=false;
/**
* Process the rewrite rules, store the results
* into self::$get
*/
public static function ProcessRewrite()
{
$URL = $_SERVER['REQUEST_URI'];
# Get everything after the .php/ and before the ?
$params = explode('.php/', $URL);
$preg_match = $params[1];
$params = explode('?', $preg_match);
$split_parameters = $params[0];
# Now check if there's anything there (we didn't just have
# index.php?query_string=...
# If that's all, then we grab a configuration setting that
# specifies the default rewrite, ie: news/showall
# Which would eq. passing index.php/news/showall
if($split_parameters == '')
{
$split_parameters = CODON_DEFAULT_MODULE;
}
# Now we split it all out, and store the peices
self::$peices = explode('/', $split_parameters);
$module_name = strtolower(self::$peices[0]);
if($module_name == '') # If it's blank, check $_GET
{
$module_name = $_GET['module'];
}
self::$current_module = $module_name;
self::$current_action = strtolower(self::$peices[1]);
unset(self::$peices[0]);
unset(self::$peices[1]);
# Restored, some addons are relying on this
$_GET['module'] = $module_name;
self::$controller = new stdClass;
self::$controller->module = $module_name;
self::$controller->controller = $module_name;
self::$controller->function = self::$current_action;
self::$controller->action = self::$current_action;
self::$controller->page = self::$current_action;
self::$params = array();
foreach(self::$peices as $peice)
{
self::$params[] = $peice;
}
# Create the object to hold all of our stuff
self::$get = new stdClass;
//self::$get->action = self::$current_action;
# If we haven't specified specific rules for a module,
# Then we use the rules we made for "default"
if(!array_key_exists($module_name, self::$rewrite_rules))
{
$module_name = 'default';
}
# This parses now the rules for a specific module
//self::ProcessModuleRewrite($module_name);
# And this tacks on our $_GET rules
parse_str($_SERVER['QUERY_STRING'], $get_extra);
$_GET = array_merge($_GET, $get_extra);
# Add the $_GET to our object
foreach($_GET as $key=>$value)
{
self::$get->$key = $value;
}
self::$run = true;
}
/**
* Add a rewrite rule for the module
*
* @deprecated
*
* @param string $module Module name
* @param array $params The rewrite rules in order array('parameter1'=>'type', 'parameter2')
* Type can be 'string', 'int', 'float', optional, blank defaults to string
* @return mixed This is the return value description
*/
/*public static function AddRule($module, $params)
{
# Clean
$set_params=array();
$module = strtolower($module);
# Format the rules, make sure we arrange
foreach($params as $key=>$value)
{
# If it wasn't done as $key=>$value, just $key,
# set the default value type as a string
if(is_numeric($key))
$set_params[$value]='string';
else
$set_params[$key]=$value;
}
self::$rewrite_rules[$module] = $set_params;
# This is for if we've already processed the rules
# once. This will allow the rules to be changed
# "on the fly", for example, inside a controller
if(self::$run == true)
{
# Reprocess the rules
#self::ProcessModuleRewrite($module);
}
}*/
/**
* Process an individual module based on the latest rules
* DEPRECATED
*
* @deprecated
*
* @param string $module_name Name of the module to re-process
* @return mixed This is the return value description
*
*/
/*public static function ProcessModuleRewrite($module_name)
{
$i=1;
# Make sure it's valid
if(is_array(self::$rewrite_rules[$module_name]))
{
# Walk through every peice of the array, $key is the
# index name, and $type is well, the type
foreach(self::$rewrite_rules[$module_name] as $key=>$type)
{
if(!isset(self::$peices[$i]))
{
continue;
}
$val = self::$peices[$i];
$i++;
# Convert to type specified
if($type == 'int')
$val = intval($val);
elseif($type == 'float')
$val = floatval($val);
# We can do any other processing we want here
# Add it both into the $_GET array, and into
# our object
self::$get->$key = $val;
$_GET[$key] = $val;
}
}
}*/
}E o meu .htacces esta da seguinte maneira:
main page of the site
DirectoryIndex index.phpprevent viewing of a specific file
<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh|tpl)$">
Order Allow,Deny
Deny from all
</FilesMatch>
prevent viewing of index
Options -Indexes#Can give without index.php for the modules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L]
E então quando acesso a URL: http://www.meusite.com.br/index.php/contatoele abre a página normalmente, porém quando eu coloco http://www.meusite.com.br/contato o site também abre normalmente, não retorna erro, mas o que acontece é que ele exibe a página inicial ao invés da página contato!
Alguém pode me ajudar?
Discussão (1)
Carregando comentários...