Codeigniter routes works in local server. when I deploy to var/www/html
of server, the routes not working as expected.
when I add index.php
before controller name in URL, it works fine.
but without index.php
, it throws 404 page not found error
.htaccess
file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I tried this as well
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
but nothing works..
It seems like mod_rewrite is not enabled in your server. If the rewrite rule isn't working, ensure you are allowing .htaccess files in your Apache config file. The directory section should contain an AllowOverride All option:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Don't forget that any changes to the Apache config file require a service restart!
Try below solution:
go to application\\config\\config.php file
Make changes
$config['index_page'] = 'index.php';
To
$config['index_page'] = '';
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /YourCIFolderName/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /YourCIFolderName/index.php [L]
</IfModule>
STEP 1
go to application/config/config.php find
$config['index_page'] = 'index.php';
change it to
$config['index_page'] = '';
STEP 2
go to application/config/routes.php
remove everything from the bottom of the page after the comment ends and paste below code
$route['default_controller'] = 'main'; // main is your default controller name
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "main/$1";
$route['admin'] = 'admin/login';
STEP 3
add a new file named as .htaccess in the root folder where you have the folders named application and system etc
add the below code and open the website
RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Header set Access-Control-Allow-Origin "*"
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
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.