[英]Shortening URLs with htaccess
我正在為我的網站使用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
我希望URL 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'] = '';
我想你可能會誤解路由和URL在CodeIgniter中的工作方式。 請參閱以下兩頁,了解如何使用路由功能:
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是方法。 對於您的URL,它將是: gallery
是控制器, photos
是方法。
如果您想傳遞任何參數,您可以將它們作為附加段附加到URL,如下所示:
/gallery/photos/param1/param2
這將實現您想要完成的更清潔的外觀。
更新我在答案中沒有提到這取代了你試圖做的.htaccess重寫。 你要保留.htaccess的這些行:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.