【问题标题】:Help me set URL using .htaccess in CodeIgniter帮助我在 CodeIgniter 中使用 .htaccess 设置 URL
【发布时间】:2011-10-26 18:37:00
【问题描述】:
我使用 CodeIgniter,我想通过 htaccess 设置 URL。我尝试了很多方法,但都没有成功。
我的文件夹结构:
--application
----corntrollers
-------public
----------home.php
----------log
-------------- login.php
-------admin
----views
-------public
----------home.tpl
----------log
-------------- login.tpl
--system
--mysite
我想设置 URL 重写:
http://localhost/mysite/index.php/log/login
=>
http://localhost/mysite/log-in
我该怎么做?
【问题讨论】:
标签:
.htaccess
codeigniter
url-rewriting
【解决方案1】:
启用Apache2的url重写模块:
a2enmod rewrite
更改/config/config.php
$config['index_page'] = "index.php";
到
$config['index_page'] = "";
还有
$config['uri_protocol'] = 'AUTO';
到
$config['uri_protocol'] = 'REQUEST_URI';
.htaccess的正确方式是:
#AddHandler application/x-httpd-php5 .php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 index.php
</IfModule>
php_flag short_open_tag on
<FilesMatch "\.(php)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>
重新启动 Apache2 服务:
sudo systemctl restart apache2.service
希望对您有所帮助。
【解决方案2】:
首先确保启用 url 重写,然后使用以下代码创建 .htaccess 文件。
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
然后在/config/config.php
/*
|--------------------------------------------------------------------------
| 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'] = 'index.php';
改变
$config['index_page']
到
$config['index_page'] = '';
下一个
/config/routes.php
$route['log-in'] = "log/log-in";