Remove index.php in code igniter for subfolder - php

I have some problem. In development I'm using php 7 and my website work well. But in production it using php 5.4 and I have problem in removing index.php. My website hosted in subfolder just like myhost/mywebsitefolder.
The problem is when using url function. example, when I accesing 'dashboard' page it will redirect to 'login' page and the url is like myhost/mywebsitefolder/login and it return 404 page not found. When I add index.php before login (myhost/mywebsitefolder/index.php/login) it work well but I must replace all of my url by adding index.php. Are there any solution. My .htaccess is just like this.
RewriteEngine on
RewriteBase /mywebsitefolder/
RewriteCond $1 !^(index.php|resources|gallery|json|slider|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Try with a slash before the dots, like:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /mywebsitefolder
RewriteCond $1 !^(index\.php|resources|gallery|json|slider|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
And in application/config/config.php and clear the index page config value:
// Old
$config['index_page'] = "index.php";
// New
$config['index_page'] = "";
Sometimes you need to enable it the rewrite module, run "apache2 enable module rewrite":
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart
Hope it works!
If no, there is others threads with the same problem,you can take a look:
- Remove index.php from codeigniter in subfolder
- Remove index.php from Codeigniter URL in subfolder with Wordpress in root
- Using htaccess to remove codeigniter index.php in a subdirectory

Related

URL-rewriting Codeigniter htaccess

Since I changed a website to another server, I have a problem that I can't access the website on domain.com/codeigniter/ . My default controller is info/
It worked with http://www.domain.com/codeigniter/info/ and http://www.domain.com/codeigniter/info/model/
$route['default_controller'] = "info/";
$route['404_override'] = '';
$route['info/(:any)'] = "info/view/$1";
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
But on the new server it doesn't work. The problem is that it always ends in http://www.domain.com/index.php and not the index.php of the directory of the codeigniter installation..
What could be the problem?
there are a few things that make CodeIgniter work in secondary folder: first of all, you must change $config['base_url'] = http://www.domain.com/codeigniter/'; in application/config/config.php.
Then, of course, change .htacces file to this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# block hidden directories
RewriteRule "(^|/)\." - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
Php mod_rewrite module is needed.
After changing the .htacces and leaving the 'base_url' empty (http://www.domain.com/codeigniter/ doesn't work).
I have the following problems:
http://www.domain.com/codeigniter/ --> ERROR 404 from CI
http://www.domain.com/codeigniter/info/ --> No input file specified
http://www.domain.com/codeigniter/index.php/info/ --> OK
EDIT: Problem solved.. I forgot a ? after the index.php in the .htaccess & I changed the default controller in routes.php to info/view/home/
It was a combination of multiple things... It is strange that it worked on the old hosting. It was obviously wrong..

how to remove index.php?file= using .htaccess to get last url

Here, I want to remove index.php?file= for every urls of my website. I have tried many tutorial of .htaccess,but unlucky to create correct one.
Actual url as below:
http://localhost/test/json/iscore2/index.php?file=home
http://localhost/test/json/iscore2/index.php?file=contact
Then I want to make like this :
http://localhost/test/json/iscore2/home
http://localhost/test/json/iscore2/contact
Probably you already have setted up rewrite module in apache2
(sudo a2enmod rewrite and host settings for AllowOverride)
I'd start with something like this in iscore2 folder
<IfModule mod_rewrite.c>
RewriteEngine on
# Play with this if redirection fails
#RewriteBase /
# if it is not a file or folder, rewrite to index.php?file=<value>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?file=$1 [L,QSA]
</IfModule>

Rewrite URL not working in CodeIgniter

I have learned and make website using CodeIgniter. I want to create this system module base so I have created multiple folders and controller,views and model for each module and it's working fine. I want to remove index.php and hide query string like pass user id in URL. I have tried htaccess code which given user guide but it did not work.
Current URL :
mysite/sitename/index.php/departments/departments/delete_department/14
Require :
mysite/sitename/departments/departments
I have tried below code in htaccess but it does not work as expected, it displays page not found error.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|fonts|js|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My directory structure is something like that
My site
|_application
| |_ Controller
| |_Login (folder) // module folder
|_Models
| |_pages
|
|_views
|_login (folder) // module folder
1) you should have Rewrite mod on apache
2) You should change application/config/config.php
$config['index_page'] = 'index.php'; to: $config['index_page'] = '';
3) after that I see that you are having a subdirectory of sitename so you should add that on your .htaccess as your RewriteBase which will result in an .htaccess like this:
RewriteEngine On
RewriteBase /sitename/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If i understand your folder structure right, then your root folder is mysite and your CI application is located at mysite/sitename . if this is the case try checking out the last part of the rewrite
RewriteRule ^(.*)$ /index.php/$1 [L]
As it currently tries to load an index file that is located on your root folder (mysite) and under my understanding your index file is actually located on mysite/sitename, so you can try this:
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
Using your FTP client, create a new file named .htaccess (including the leading dot) in the same folder as your site’s main index.php file.
Then add the following code to this newly created .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
do you have apache rewrite-mod enable ?
you must have apache rewrite-mod enabled to do that ..
Check your config at apache configuration path ..
Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
change $config['index_page'] = 'index.php'
to
$config['index_page'] = ''
from
application\config\config.php
Also check your apache config for mod rewrite is enabled or not.
Checking URl Rewrite is Working or Not
MOD Rewrite
$isTrue = in_array('mod_rewrite', apache_get_modules());
Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.
You can test it using the following, though
$isTrue = strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false
If the above evaulates to true, then mod_write is enabled.
You are using the rewrite engine of apache. Make sure mod_rewrite is enabled and make sure your regular expression fulfills your needs.
Read about mod_rewrite here.
The htaccess rules look OK. Because you havent shown your code its hard to say but its either one of the following:
Your having an issue with your controller and method both being called 'departments'. If this is true either try renaming the method to something other than 'departments' or getting rid of it all together.
You dont have a controller called 'departments' (it doesnt show in your directory structure) or a route to handle the URI 'departments'. Either create the controller or add a route to direct the URI 'departments' to the controller you want to use. ie:
$route['departments/departments']='an_existing_controller/an_existing_method';
You dont have a method called 'departments'. If this is true get rid of the additional URI component called 'departments' or create a new method (as above dont call your method the same thing as your controller).
In config.php (which is mostly loated in application/config/config.php
make index page value to empty string, like this,
$config['index_page'] = '';
In root folder of your application make a .htaccess file with following code,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

Removing index.php from url works on localhost and doesn't work online

I work on PHP project using codeigniter, I remove the index.php from URL locally, here is my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
When I upload the project online routes don't work at all, and I need to set index.php with plain url instead of the route!
for example instead of writing index I have to write
index.php/product_management_controller/index
If I write index it doesn't work even if I update the route to include index.php !! That means the routes are also not working!
my current route is
$route['index'] = "product_management_controller/index";
And here's a few things I've set in my config.php file:
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
What is the solution!!
It works properly locally!!
Your server that your hosting on will need to allow .htaccess files, in your Apache config you will need to add:
AllowOverride All
to your Apache config. Then restart the Apache server.
You could also try
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
There is an documentation for removing index.php from url on codeigniter
https://ellislab.com/codeigniter/user-guide/general/urls.html
By the way you are on godaddy shared server as per the comments and you don't have access to apache config file, but the good news is you don't need to worry rewrite mode is on for you by default. :)

CodeIgniter Error 404 on root domain using PHP CGI

So, today I upgrade PHP and apache on my server to version PHP 5.4 and Apache 2.4.
And then I noticed on one domain using older version of codeigniter (I've try to echo CI_VERSION but it shows nothing) is showing error 404 on the root domain. But typing domain.com/home works fine. (home is my default controller). Typing any other controller also working fine. But domain.com shown error 404.
.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
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]
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|apc\.php|assets|nivo-slider|hr|stm|elearning|docs|adminer|favicon\.ico|file|flash|tinymce|robots\.txt)
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 /application/errors/404.php
</IfModule>
config.php
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
routes.php
$route["default_controller"] = "home";
$route["error_controller"] = "error";
$route['scaffolding_trigger'] = "";
I've trying change uri_protocol to QUERY_STRING & REQUEST_URI but still not working.
I managed to solve this problem by adding DirectoryIndex home on my .htaccess.

Categories