CodeIgniter removing index.php from url

前端 未结 30 1439
[愿得一人]
[愿得一人] 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 11:55
    + Copy .htaccess from Application to Root folder
    + edit .htaccess and put
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ci_test/
    
        #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]
    
        #When your application folder isn't in the system folder
        #This snippet prevents user access to the application folder
        #Submitted by: Fabdrol
        #Rename 'application' to your applications folder name.
        RewriteCond %{REQUEST_URI} ^application.*
        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>
    
    Note: make sure your web server is enable for  "rewrite_module"
    
    + Remove index.php from $config['index_page'] = 'index.php';
    - edit config.php in application/config
    - search for index.php
    change it from  $config['index_page'] = 'index.php'; to  $config['index_page'] = '';
    

    Please watch this videos for reference https://www.youtube.com/watch?v=HFG2nMDn35Q

    0 讨论(0)
  • 2020-11-21 11:56

    Use this code. Change the 2nd line as you have your folder name. Where index.php file and folders application, system folder lie. Mostly problems happen due to second line. we don't configure the folder name where project is place. Important is 2nd line. This is simple to remove the index.php. RewriteEngine on RewriteBase /project_folder_name RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L]

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

    Go to controller/config.php file.

    config['index_url']='';

    Reference for more study.

    https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html

    https://ellislab.com/blog/entry/fully-removing-index.php-from-urls

    0 讨论(0)
  • 2020-11-21 11:57

    Try the following

    Open config.php and do following replaces

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

    to

    $config['index_page'] = ""
    

    In some cases the default setting for uri_protocol does not work properly. Just replace

    $config['uri_protocol'] ="AUTO"
    

    by

    $config['uri_protocol'] = "REQUEST_URI"
    

    .htaccess

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
    

    Note: .htaccess code vary depending on hosting server. In some hosting server (e.g.: Godaddy) need to use an extra ? in the last line of above code. The following line will be replaced with last line in applicable case:

    // Replace last .htaccess line with this line
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
    
    0 讨论(0)
  • 2020-11-21 11:57

    Minor thing: (Optional)

    Try restarting your Apache after updating the rewrite engine status in vhosts as Rewrite ON.

    One critical thing to notice:

    In Apache vhosts file, check whether you have this script line AllowOverride None If yes, please remove/comment this line.

    My vhosts file script: (Before)

    <VirtualHost *:80>
        DocumentRoot "/path/to/workspace"
        ServerName ciproject
        ErrorLog "/path/to/workspace/error.log"
        CustomLog "/path/to/workspace/access.log" common
        Directory   "/path/to/workspace">
            Options Indexes FollowSymLinks
            AllowOverride None
            Order allow,deny
            Allow from all
        </Directory> 
    </VirtualHost>
    

    My vhosts file script: (After)

    <VirtualHost *:80>
        DocumentRoot "/path/to/workspace"
        ServerName ciproject
        ErrorLog "/path/to/workspace/error.log"
        CustomLog "/path/to/workspace/access.log" common
        Directory   "/path/to/workspace">
            Options Indexes FollowSymLinks
            #AllowOverride None
            Order allow,deny
            Allow from all
        </Directory> 
    </VirtualHost>
    

    I faced the same problem, struggled for around 1 day, all were fine. Later, by trial and error method, found this thing. After removing this, my job was accomplished. The URL now does not look up for index.php :)

    0 讨论(0)
  • 2020-11-21 11:59

    Step:-1 Open the folder “application/config” and open the file “config.php“. find and replace the below code in config.php file.

    //find the below code   
    $config['index_page'] = "index.php" 
    //replace with the below code
    $config['index_page'] = ""
    

    Step:-2 Go to your CodeIgniter folder and create .htaccess

    Path:
    Your_website_folder/
    application/
    assets/
    system/
    .htaccess <——— this file
    index.php
    

    Step:-3 Write below code in .htaccess file

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

    Step:-4 In some case the default setting for uri_protocol does not work properly. To solve this problem just open the file “application/config/config.php“, then find and replace the below code

    //Not needed for CodeIgniter 3 its already there.
    //find the below code
    $config['uri_protocol'] = "AUTO"
    //replace with the below code
    $config['uri_protocol'] = "REQUEST_URI" 
    

    Thats all but in wamp server it does not work because rewrite_module by default disabled so we have need to enable it. for this do the following

    1. Left click WAMP icon
    2. Apache
    3. Apache Modules
    4. Left click rewrite_module

    Original Documentation

    Example Link

    0 讨论(0)
  • 2020-11-21 11:59

    There are two steps:

    1) Edit config.php

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

    To

    $config['index_page'] = '’;
    

    2) Create/Edit .htaccess file

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