开发者

Help me set URL using .htaccess in CodeIgniter

I use CodeIgniter and I want to set URL by htaccess. I have tried many ways but have not succeeded yet.

My folder structure :

--application
----corntrollers
-------public
----------home.php
----------log
-------------- login.php
-------admin
----views
-------public
----------home.tpl
----------log
-------------- login.tpl
--system
--mysite

I want to set URL to rewrite :

http://localhost/mysite/index.php/log/login
=>
http://loca开发者_如何学Clhost/mysite/log-in

How can I do that ?


Enable url rewriting module of Apache2:

a2enmod rewrite 

Change /config/config.php

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

to

$config['index_page'] = ""; 

As well

$config['uri_protocol'] = 'AUTO';

to

$config['uri_protocol'] = 'REQUEST_URI';

The correct way of .htaccess is:

#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 Service:

sudo systemctl restart apache2.service

Hope that helps.


First of all make sure url rewriting is enabled and then create .htaccess file with code below.

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Then in /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';

change

$config['index_page']

to

$config['index_page'] = '';

Next in

/config/routes.php

$route['log-in'] = "log/log-in";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜