1

我正在为我的网站使用 CodeIgniter,我想缩短我的 URL 以使其看起来更好,但它似乎不起作用。我正在使用 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

我希望 URLgicfamily.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'] = '';
4

1 回答 1

3

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

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

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

/gallery?var=photos

会成为

/gallery/photos

In this example each of these is called "segments" of the URL. gallery is segment #1 and photos is segment #2. The pattern always follows that segment #1 is the controller and segment #2 is the method. For your URL it would be: gallery is the controller, and photos is the method.

If you want to pass any parameters you would append them on as additional segments to the URL like so:

/gallery/photos/param1/param2

This would accomplish the cleaner look you're trying to accomplish.


Update I didn't mention in the answer that this replaces the .htaccess rewrites you're trying to do. You'd keep these lines of the .htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
于 2012-09-18T01:00:06.653 回答