我使用CodeIgniter,我想通过htaccess设置URL。我尝试了很多方法,但还没有成功。
我的文件夹结构:
--application
----corntrollers
-------public
----------home.php
----------log
-------------- login.php
-------admin
----views
-------public
----------home.tpl
----------log
-------------- login.tpl
--system
--mysite
我想将网址设置为重写:
http://localhost/mysite/index.php/log/login
=>
http://localhost/mysite/log-in
我该怎么做?
答案 0 :(得分:2)
启用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>
Restar Apache2服务:
sudo systemctl restart apache2.service
希望有所帮助。
答案 1 :(得分:0)
首先确保启用了网址重写,然后使用下面的代码创建.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";