简体   繁体   中英

404 error while trying to access codeigniter url's without index.php

Hi i recently changed my hosting to AWS. I created an instance and hosted my website and trying to access it same like old hosting. But i am not able to access it when i tried to access inner pages without index.php in the url. But if i used index.php it's working fine. I tried different configurations in .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]


<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

Right now my config file configurations are like this below.

$config['base_url']     = 'http://www.cofounder.in/';

/*
|--------------------------------------------------------------------------
| 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?';

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'                        Default - auto detects
| 'PATH_INFO'           Uses the PATH_INFO
| 'QUERY_STRING'        Uses the QUERY_STRING
| 'REQUEST_URI'         Uses the REQUEST_URI
| 'ORIG_PATH_INFO'      Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';

Please make sure your " mod_rewrite " is On. Might be this is issue.

make change in:

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

to

$config['index_page'] = '';

.htaccess

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://abc.com/$1 [L,R=301] 
# you want to restrict from url
RewriteCond $1 !^(index\.php|images|mobile|templates|favicon\.ico|robots\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
 # can give your server port number not necessary
RewriteCond %{SERVER_PORT} 443

change

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

to

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

then use this code in your htaccess file

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
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.
    ErrorDocument 404 /index.php
</IfModule>

SOmetimes you may need to change

RewriteRule ^(.*)$ index.php/$1 [L]

to

RewriteRule ^(.*)$ index.php/?$1 [L]

you may also need to change

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

by putting the other protocols mentioned above in the config.php file

Here is my .htaccess try this if possible

<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]

    #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
    RewriteCond $1 !^(index\.php|images|stylesheets|javascript)
    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> 

In your config change it to :

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

I had this problem some time before and this works for me by changing in my apache sites AllowOverride None ==> AllowOverride All in /etc/apache2/sites-available

In Ubuntu(as yours is a Ubuntu system)

Step-1

Disable the site in apache

sudo a2dissite yoursitename (In my case it was default)

Step-2

Edit the site

sudo vi yoursitename (you can use other editors like nano instead of vi)

and replace

AllowOverride None ==> AllowOverride All

Your virtual host should ultimately look something like this:

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/vhosts/example.com
  <Directory /var/www/vhosts/example.com>
    AllowOverride all
  </Directory>
</VirtualHost>

Step 3

Enable the site

sudo a2ensite default

Step-4

Restart Apache

sudo service apache2 restart

Now you check your app. it might work.

Please let me know if you face any issues in this.

A same kind of discussion here that might helpful

Make change in:

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

to

$config['index_page'] = '';

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

Options All -Indexes

save this .htaccess file in your root folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM