【问题标题】:Rewrite Url with apache2用 apache2 重写 Url
【发布时间】:2010-11-11 13:40:04
【问题描述】:

我正在试验 CodeIgniter PHP 框架,这个框架的工作原理是:

http://localhost:7777/~dhalsim/ci/index.php/blog

所以,我尝试从那里删除 index.php 部分。到目前为止,我做了这些:

  1. $config['index_page'] = "index.php"; 改为$config['index_page'] = "";

  2. $config['uri_protocol'] = "AUTO"; 制作$config['uri_protocol'] = "REQUEST_URI";

  3. 通过“a2enmod rewrite”启用 apache mod_rewrite

  4. 将 .htaccess 文件放入/ci 目录:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
  5. 当然还要重启 apache 服务器

这是我的 apache 日志,配置如下:

127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/~dhalsim/ci/blog' pattern='^system.*' => not-matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-f' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-d' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] rewrite 'blog' -> 'index.php?/blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) split uri=index.php?/blog -> uri=index.php, args=/blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] add per-dir prefix: index.php -> /home/dhalsim/public_html/ci/index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] trying to replace prefix /home/dhalsim/public_html/ci/ with /
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (5) strip matching prefix: /home/dhalsim/public_html/ci/index.php -> index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) add subst prefix: index.php -> /index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (1) [perdir /home/dhalsim/public_html/ci/] internal redirect with /index.php [INTERNAL REDIRECT]

这是 Firefox 中的结果:

404 未找到:
在此服务器上未找到请求的 URL /index.php。


那么,我应该怎么做(或者我哪里错了)才能让这些 URL 工作? http://localhost:7777/~dhalsim/ci/blog/ 而不是 http://localhost:7777/~dhalsim/ci/index.php/blog/

【问题讨论】:

    标签: apache mod-rewrite codeigniter


    【解决方案1】:

    我是这样解决的:

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

    然后在config.php文件中

    /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |   http://example.com/
    |
    */
    $config['base_url'] = "http://localhost:7777/~dhalsim/ci/";
    
    /*
    |--------------------------------------------------------------------------
    | Index File
    |--------------------------------------------------------------------------
    |
    | Typically this will be your index.php file, unless you've renamed it to
    | something else. If you are using mod_rewrite to remove the page set this
    | variable so that it is blank.
    |
    */
    $config['index_page'] = "";
    

    更新

    在 routes.php 文件中:

    //Point to default controller
    $route['default_controller']    = "site/home"; //Set to your controller
    
    // Point to the right controller
    $routing_array = array(
                'blog' => 'site/blog',
            ); // set to your blog controler
    

    【讨论】:

    • $config['uri_protocol'] = "AUTO";
    • 其实我的 $route['default_controller'] 是 $config['base_url'] = "http://".$_SERVER['HTTP_HOST']; $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);和 $route['default_controller'] = "blog"; //我的控制器,但没有得到 $routing_array 的用途。
    【解决方案2】:

    使用以下重写规则:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*)$ /index.php/$1 [NC,L]
    

    为了让它工作,你应该在不同的端口上设置你的 CodeIgniter 应用程序或使用 /etc/hosts 给它一个简单的虚拟主机(127.0.0.1 cihttp://ci:7777/ Apache 设置为使用 ci 虚拟主机。 )。然后您可以将基本 url 设置为 / 而不必担心。否则你将不得不处理令人困惑的目录问题和 mod_rewrite。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 2021-11-14
      • 2018-02-26
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多