简体   繁体   English

使用htaccess缩短URL

[英]Shortening URLs with htaccess

I am using CodeIgniter for my site and I want to shorten my URLs to look a lot nicer, but it doesn't seem to work. 我正在为我的网站使用CodeIgniter,我想缩短我的网址以使其看起来更好,但它似乎不起作用。 I am using the htaccess method and this is my code below. 我使用的是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

I want the URL gicfamily.org/gallery?var=photos to look like gicfamily.org/gallery/photos . 我希望URL gicfamily.org/gallery?var=photos看起来像gicfamily.org/gallery/photos

My config file looks something like this: 我的配置文件看起来像这样:

<?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'] = '';

I think you may be misunderstanding how routes and URLs work in CodeIgniter. 我想你可能会误解路由和URL在CodeIgniter中的工作方式。 Please see the following two pages that speak to how to use the routing features: 请参阅以下两页,了解如何使用路由功能:

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

The idea of the "parameter" is implied by the parameter's placement in the URL structure. 参数在URL结构中的位置暗示了“参数”的概念。 For example: 例如:

/gallery?var=photos

Would become 会成为

/gallery/photos

In this example each of these is called "segments" of the URL. 在此示例中,每个都称为URL的“段”。 gallery is segment #1 and photos is segment #2. gallery是第1段, photos是第2段。 The pattern always follows that segment #1 is the controller and segment #2 is the method. 模式总是遵循段#1是控制器,段#2是方法。 For your URL it would be: gallery is the controller, and photos is the method. 对于您的URL,它将是: gallery是控制器, photos是方法。

If you want to pass any parameters you would append them on as additional segments to the URL like so: 如果您想传递任何参数,您可以将它们作为附加段附加到URL,如下所示:

/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. 更新我在答案中没有提到这取代了你试图做的.htaccess重写。 You'd keep these lines of the .htaccess: 你要保留.htaccess的这些行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM