Переписать URL с помощью apache2

Я экспериментирую с PHP-фреймворком CodeIgniter, этот фреймворк работает так:

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

Итак, я попытался удалить index.php часть оттуда. Пока делаю такие:

  1. сделать $config['index_page'] = "index.php"; до $config['index_page'] = "";

  2. сделать $config['uri_protocol'] = "REQUEST_URI"; из $config['uri_protocol'] = "AUTO";

  3. включить apache mod_rewrite с помощью "a2enmod 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 Not Found:
Запрошенный URL-адрес /index.php не найден на этом сервере.


Итак, что я должен сделать (или где я ошибаюсь), чтобы заставить работать эти URL-адреса? http://localhost:7777/~dhalsim/ci/blog/ вместо http://localhost:7777/~dhalsim/ci/index.php/blog/


person dhalsim    schedule 16.07.2009    source источник


Ответы (2)


Вот как я это решаю:

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

ОБНОВЛЕНИЕ

В файле route.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
person Shadi Almosri    schedule 16.07.2009
comment
$config['uri_protocol'] = АВТО; - person Shadi Almosri; 17.07.2009
comment
на самом деле мой $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. - person dhalsim; 17.07.2009

Используйте следующее правило перезаписи:

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 ci и http://ci:7777/ с Apache, настроенным на использование ci виртуального хоста.). Затем вы можете установить базовый URL-адрес / и не беспокоиться об этом. В противном случае вам придется иметь дело с запутанными проблемами каталогов и mod_rewrite.

person coreyward    schedule 18.06.2010