使用htaccess缩短URL

时间:2012-09-13 15:05:27

标签: .htaccess codeigniter

我正在为我的网站使用CodeIgniter,我希望缩短我的网址以使其看起来更好,但它似乎不起作用。我正在使用htaccess方法,这是我的代码。

<IfModule mod_rewrite.c>

RewriteCond $1 !^(index\.php|(.*)\.swf|user_guide|profiles|images|min|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteEngine On

RewriteBase /gicfamily/

RewriteRule ^([a-zA-Z0-9]+)$ gallery?var=$1

我想要网址gicfamily.org/gallery?var=photos 看起来像gicfamily.org/gallery/photos

我的配置文件如下所示:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


$config['base_url'] = '';


$config['index_page'] = '';


$config['uri_protocol'] = 'AUTO';

$config['url_suffix'] = '';

$config['language'] = 'english';


$config['charset'] = 'UTF-8';


$config['enable_hooks'] = FALSE;


$config['subclass_prefix'] = 'MY_';



$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';


$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = FALSE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd'; // experimental not currently in use


$config['log_threshold'] = 0;


$config['log_path'] = '';

$config['log_date_format'] = 'Y-m-d H:i:s';

$config['cache_path'] = '';


$config['encryption_key'] = 'ksfkvjbskfbvskbvlbsbvsokbskdb';


$config['sess_cookie_name']     = 'gic_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'gic_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;

$config['global_xss_filtering'] = FALSE;

$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;

$config['compress_output'] = FALSE;


$config['time_reference'] = 'local';


$config['rewrite_short_tags'] = FALSE;


$config['proxy_ips'] = '';

1 个答案:

答案 0 :(得分:3)

我认为您可能误解了CodeIgniter中路由和URL的工作方式。请参阅以下两页,了解如何使用路由功能:

http://codeigniter.com/user_guide/general/urls.html http://codeigniter.com/user_guide/general/routing.html

参数在URL结构中的位置暗示了“参数”的概念。例如:

  

/gallery?var=photos

会变成

  

/gallery/photos

在此示例中,每个都称为URL的“段”。 gallery是第1段,photos是第2段。模式总是遵循段#1是控制器,段#2是方法。对于您的网址,它是:gallery是控制器,photos是方法。

如果你想传递任何参数,你可以将它们作为附加片段附加到URL,如下所示:

  

/gallery/photos/param1/param2

这将实现您想要完成的更清洁的外观。


更新 我没有在答案中提到这取代了你试图做的.htaccess重写。你要保留.htaccess的这些行:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]