How do I write a .htaccess file to make CodeIgniters URL routing work?

后端 未结 9 1112
死守一世寂寞
死守一世寂寞 2020-12-01 01:13

I\'m running a LAMP environment with CodeIgniter. I want to be able to use its URL pattern, like, http://localhost/controller/function/ID, but by default it has

相关标签:
9条回答
  • 2020-12-01 01:30

    In your system/application/config/config.php, change

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

    to

    $config['index_page'] = "";
    

    and in your .htaccess, add this:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    Add your .htaccess file to the same directory where the system folder is located, so in your CodeIgniter root directory, you should have

    • system/
    • user_guide/
    • index.php
    • .htaccess
    • license.txt

    Also, since you have multiple sites running on your server, you might want to have VirtualHosts. Add this to the last part your apache2.conf for each site that you have:

    Listen *:11000
    <VirtualHost *:11000>
         ServerAdmin you@somewhere.com
         DocumentRoot "/var/www/cms"
         ServerName you-dummy.com
         <Directory "/var/www/cms">
              AllowOverride All
              Options +Indexes
              DirectoryIndex index.php
              Order allow,deny
              Allow from all
         </Directory>
         ErrorLog logs/cms_log
         CustomLog logs/cms_log common
    </VirtualHost>
    

    You may now access the site from http://localhost:11000/ and access the controller using http://localhost:11000/hello.

    0 讨论(0)
  • 2020-12-01 01:30

    Put that at the document root of your Web site.

    If you have multiple sites, put it at the top of each such site.

    If you have multiple sites running from the same directory, you can use a RewriteCond to exclude or include particular sites to the rewrite rule.

    You can exclude certain directories (eg /articles) the same way.

    0 讨论(0)
  • 2020-12-01 01:33

    Had some problems with finding where to change the "AllowOverRide All".

    From this guide I found the (default) file.

    Remove Index.php CodeIgniter URL On Ubuntu

    1. Preliminary Note. I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

      sudo su
      
    2. Enable mod_rewrite module on apache. First enable the module as follows:

      a2enmod rewrite
      

      Change all occurrence of "AllowOverRide None" to "AllowOverRide All". Basically all "None" to "All" in the following file:

       gedit **/etc/apache2/sites-available/default**
      

      Restart Apache:

      /etc/init.d/apache2 restart
      
    3. Open file config.php on CodeIgniter (application\config). Remove index.php on $config['index_page'] = "index.php";

      $config['index_page'] = "";
      
    4. Make file .htaccess on CodeIgniter root directory. Fill the file with this code :

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

    Now Index.php on CodeIgniter URL disappear

    0 讨论(0)
  • 2020-12-01 01:37
    <IfModule rewrite_module>
    RewriteEngine on
    RewriteBase /
    
    # Removes the "/" at the end
    RewriteRule (.+)/$ /$1 [L,R]
    
    RewriteCond $1 !^(index\.php|dhtml|img|css|js|favicon\.ico|robots\.txt)
    RewriteRule ^([^?&]+) /index.php/$1 [L]
    </IfModule>
    
    0 讨论(0)
  • 2020-12-01 01:37
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    

    The simple one in .htaccess on root folder! Works for me.

    0 讨论(0)
  • 2020-12-01 01:38

    Try this one, it works perfetct

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  
    
    0 讨论(0)
提交回复
热议问题