CodeIgniter removing index.php from url

前端 未结 30 1443
[愿得一人]
[愿得一人] 2020-11-21 11:51

My current urls look like this [mysite]index.php/[rest of the slug].
I want to strip index.php from these urls.

mod_rewrite

相关标签:
30条回答
  • 2020-11-21 12:00

    Step 1 => Place your .htaccess file in root folder where application and system folders exist.

    Step 2 => If your web path using sub-folder like - yourdomain.com/project/ - then use following code in htaccess file

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

    If your web path using root-folder like - yourdomain.com - then use following code in htaccess file

    RewriteEngine on
    RewriteBase    /
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    0 讨论(0)
  • 2020-11-21 12:01

    Step 1 :

    Add this in htaccess file

    <IfModule mod_rewrite.c>
      RewriteEngine On
      #RewriteBase /
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [QSA,L]
    </IfModule>
    

    Step 2 :

    Remove index.php in codeigniter config

    $config['base_url'] = ''; 
    $config['index_page'] = '';
    

    Step 3 :

    Allow overriding htaccess in Apache Configuration (Command)

    sudo nano /etc/apache2/apache2.conf
    

    and edit the file & change to

    AllowOverride All
    

    for www folder

    Step 4 :

    Enabled apache mod rewrite (Command)

    sudo a2enmod rewrite
    

    Step 5 :

    Restart Apache (Command)

    sudo /etc/init.d/apache2 restart
    
    0 讨论(0)
  • 2020-11-21 12:01

    You can eliminate index.php from URL by create .htaccess file in the base directory of your porject, that file its content as

    RewriteEngine on
    RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
    RewriteCond %(REQUEST_FILENAME) !-f
    RewriteCond %(REQUEST_FILENAME) !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L]
    

    save that file also your changed in config.php file is right

    $config['index_page'] = '';
    

    I wish to run successfully

    0 讨论(0)
  • 2020-11-21 12:02

    Besides these answers, one can just, add index.php or edit it (if it is already there) - for security you want to disable index.php anyway, from listing the files in your website directory - then change the content into

    <?php
       header('location:your_required_starting.php'); 
    ?>
    
    0 讨论(0)
  • 2020-11-21 12:03

    Well these all answers are good but for a newbie(which is do first time) these are confusing. There are other htaccess files which is reside in controller but we have to edit or add the htaccess in main project folder.

    This is your codeigniter folder, where file resides like application,system,assets,uploads etc.

    Your_project_folder/
    application/
    assets/
    cgi-bin/
    system/
    .htaccess---->(Edit this file if not exist than create it)
    index.php
    

    Change this file as written below:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
      #  slashes.
      # If your page resides at http://www.example.com/mypage/test1
      # then use RewriteBase /mypage/test1/
      # Otherwise /
      RewriteBase /
      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: Anand Pandey
      ErrorDocument 404 /index.php
    </IfModule>
    

    and yes of course you need to change two lines of code in the application/config/config.php

    //find the below code   
    $config['index_page'] = "index.php";
    $config['uri_protocol'] ="AUTO" 
    //replace with the below code
    $config['index_page'] = "";
    $config['uri_protocol'] = "REQUEST_URI";
    
    0 讨论(0)
  • 2020-11-21 12:03

    This works for me. Tested in codeigniter 3.1.11, PHP 5.6.40 I use a virtualhost in my apache2 config in testing server.

    <VirtualHost *:80>
         ServerAdmin webmaster@dummy-host2.example.com
         DocumentRoot (__DIR__)
         ServerName mydomain
         ##ErrorLog "logs/dummy-host2.example.com-error.log"
         ##CustomLog "logs/dummy-host2.example.com-access.log" common
         <Directory "(__DIR__)">
            Require all granted
            AllowOverride All
       </Directory>
    </VirtualHost>
    

    And /.htaccess content

        <IfModule mod_rewrite.c>
           RewriteEngine On
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteRule ^(.*)$ index.php/$1 [L] 
        </IfModule>
    
    0 讨论(0)
提交回复
热议问题