Cannot remove index.php from CodeIgniter URL - .htaccess

My directory structure is /var/www/CI/ with all the folders viz., application, system under the folder CI. Have created a .htaccess file under CI.
Following is the code in .htacess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Still localhost/CI/blog gives 404 error. Can anyone guide as to where this rule is wrong?

Please try the following
At first, make sure you set the config file like the following..
$config['index_page'] = '';
Also make sure that mod_rewrite is enabled in the httpd.conf file and after that, overwrite your .htaccess file which is located your project root folder not in the application folder with the following code..
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Make sure in your application/config/config.php file this is set as follows:
$config['index_page'] = '';
Also do this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^CI/(index\.php|public|images|robots\.txt|css)
RewriteRule ^CI/(.*)$ CI/index.php/$1 [L]
</IfModule>
Typically you don't put CI in its own directory and put application and system in the root folder.

Check your Apache configuration so that it is allowing an override.
If the AllowOverride is set to None, the rewrite won't work for you.
Adding the following to your httpd.conf (NOT your .htaccess) should fix your problem
<Directory "/">
AllowOverride All
</Directory>
Let me know if it's still not working.

This works. Although, please make sure your mod_rewrite is enabled,
Find the httpd.conf file under the C:\wamp\bin\apache\apache2.4.9\conf folder inside the Apache’s installation folder.
Find the following line #LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file.You can do this easily by searching the keyword “mod_rewrite”.
Remove the # at the starting of the line, # represents that line is commented.
Then restart the apache server.
You can see now mod_rewrite in the Loaded Module section while doing phpinfo().

Use the following steps,
1. Create .htaccess file in the document root [myroot_folder/.htaccess].
2. open the config file in application/config/config.php and do the following
$config['index_page'] = '';
remove index.php from your base url
if it is $config['base_url']= 'http://localhost/myroot_folder/index.php';
change it to $config['base_url']= 'http://localhost/myroot_folder/';
Now open the .htaccess file and add the following code block
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

1) Make .htaccess file and put this code.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
2) Change:
$config['index_page'] = '';
remove index.php present in config.php file
3) Rewrite on from your server

If you have not worked all but that you did so, try this:
Change AllowOverride None to AllowOverride All in the virtual host file in /etc/apache2/sites-available/default
Details here

Related

Removing index.php on CodeIgniter 2.2.0

I am running Apache 2.2.22 on Ubuntu 12.04.3 (Oracle VM on Windows 8.1).
Using the PHP phpinfo() function I see mod_rewrite in the apache2handler section under Loaded Modules, so mod_rewrite is enabled.
I made the following changes to my CI config file (application/config/config.php),
$config['index_page'] = 'index.php'; to: $config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; to: $config['uri_protocol'] = 'PATH_INFO';
I have tried numerous versions this(.htaccess file located in the root directory of CI ):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
but have yet to get it to work.
What else am I missing here?
This worked!
RewriteEngine On
RewriteBase /testsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Working on the assumption that your folder structure is something like... /var/www/html/testsite and your url is localhost/testsite, then the above rewrite rule will be looking for index.php in /var/www/html and not /var/www/html/testsite.
( /var/www/html will be the default document root. )
So in your equivalent folder of /var/www/html/testsite you can try using the following in your .htaccess file located in /var/www/html/testsite/. so you'd have /var/www/html/testsite/.htaccess with the code...
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
All I have done is removed the leading / in the rewrite rule in the last line so it's not heading back to the default document root.

I want to remove index.php from codeigniter url

I want to remove index.php from codeigniter url
my current configurations I tried so far with zero results are
folder structure:
xampp\htdocs\Codeigniter\application\controllers\olx\olx.php
xampp\htdocs\Codeigniter\application\views\olx\frmSignup
my HTACCESS file on the location
xampp\htdocs\Codeigniter.HTACCESS with the following contents nothing else in .HTACCESS file for me
RewriteEngine On
RewriteBase /olx
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
mod_rewrite is enabled checked using phpinfo();
following changes made to config.php file
$config['base_url'] = 'http://mypc/olx';
$config['index_page'] = '';
when try to access this url
http://mypc/codeigniter/olx/frmSignup i get 404 error
changes made to routes.php are
$route['default_controller'] = 'olx';
$route['olx/frmSignup'] = 'olx/frmSignup';
please help I want to access my app without index.php. i am working on localhost with Windows7
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/000-default.conf in ubuntu
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default.conf in windows
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All <---- replace None with All
</Directory>
<Directory /var/www >
Options Indexes FollowSymLinks MultiViews
AllowOverride All <--- replace None with All
Order allow,deny
allow from all
</Directory>
....
Then put this code in .htaccess file in root folder in codeigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
It will definately work.
From http://ellislab.com/codeigniter%20/user-guide/general/urls.html - Try
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
after all said by friends, you have to do another thing (I think you are doing it on localhost):
First, double check the suggestions:
then go to windows tray, right click on WAMP icon, and then click on it , go to Apache menu, and then to modules and then check for rewrite_module to see if it is checked or not, and click on it if you find it unchecked (means disabled) to enable it. Now try again and report it...
You just have to create .htaccess file on the root application folder or edit it if you already have. The following .htaccess file works for me.
.htaccess
# BEGIN ci
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Codeigniter
#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>
so not olx as it's a controller and not a folder
in config
$config['base_url'] = '';
$config['index_page'] = '';
in route
$route['default_controller'] = "olx";
and move your
D:\xampp\htdocs\Codeigniter\application\controllers\olx\olx.php
to
D:\xampp\htdocs\Codeigniter\application\controllers\olx.php
and olx should look like
class olx extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "test";
// or $this->load->view('olx/overview.php', $data);
}
}
and try localhost/codeigniter/ or localhost/codeigniter/olx

How to remove index.php from slim framework URL

i'm trying to remove index.php form an URL:
this works
http://server/bw/index.php/test
this doesn't work
http://server/bw/test
i try to change .htaccess and watching on web i see that it should be like this:
RewriteEngine On
RewriteBase /bw/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
i try editing it in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
or in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /bw/index.php [QSA,L]
or in this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
But when i try to access to http://server/bw/test it says me:
Not Found
The requested URL /bw/test was not found on this server.
Apache/2.2.15 (CentOS) Server at server Port 80
I check that inside my httpd.conf LoadModule rewrite_module modules/mod_rewrite.so is enable.. i don't know what to do now..
how can i solve? please help me!
Try this, which used e.g. in WordPress
RewriteRule . index.php [L]
or this, which is used by e.g. Lavavel PHP Framework
RewriteRule ^(.*)$ index.php/$1 [L]
You might also consider adding
RewriteCond %{REQUEST_FILENAME} !-d
before the RewriteRule to also exclude existing directories, not only existing files. But that's up to you.
In my case I updated AllowOverride All ,
then run sudo a2enmod rewrite to avoid Internal 500 error then restart Apache service apache2 restart
For me, I got things to work using this line from Dehalion's answer:
RewriteRule . index.php [L]
So the index.php (or any xyz.php) file is not seen in the request url
http://localhost/demo1/mycompany/hello/Jim
With the following caveats:
You have this route defined:
$app->get('/mycompany/hello/:name', doHello );
The root element (for the route /mycompany/..) is also the name of the file.
That is, the route exists in a file called "mycompany.php"
Yes, it's a bit of a hack ... but since I find apache config confusing/intimidating :) ... I figure this solution is stable enough to satisfy the requirements.

CodeIgniter won't run in a subdirectory

I have a CodeIgniter install running in our root web directory that I copied into a subdirectory called /dev... I edited the config/config.php file to reflect the change so it is now like this:
$config['base_url'] = 'http://mysite.com/dev';
$config['sbase_url'] = 'https://mysite.com/dev';
$config['default_email'] = 'noreply#mysite.com';
$config['site_url'] = 'http://mysite.com/dev';
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
When I hover over any links on the site they are correct, for example the contact us page link reads www.mysite.com/dev/contact but when I click any of the links I get a 404 error...
Is there something common I am missing?
Enable FollowSymlinks & add a ./ in front of index.php in your RewriteRule:
<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>
Also, I would leave blank $config['base_url'] = ''; & use the base_url() url helper when constructing links instead, which makes moving environments easier (e.g. subdir'd dev & root-level production).
... and of course, insure that ModRewrite is enabled in your Apache config.

Puzzled using codeigniter on shared hosting server

This ones a new on me. I'm transferring a site I made in codeigniter to a godaddy shared hosting server. I can get to the home page with:
http://www.example.com
But when I try to go to the other pages like:
http://www.example.com/about-us/
It throws me this error message:
Not Found
The requested URL /example/index.php/about-us/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
If I use:
http://www.example.com/index.php/about-us/
The page appears as clear as day.
I have looked up CI workaround , but my site is more of static pages then dynamic pages.
Overall, my question is, what am I doing wrong and does this have anything to do with having a godaddy shared hosting account?
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
CI Config file
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'QUERY_STRING';
CI autoload file
$autoload['helper'] = array('url');
09-30-2011 Update:
In the .htaccess use:
# Options
Options -Multiviews
Options +FollowSymLinks
#Enable mod rewrite
RewriteEngine On
#the location of the root of your site
#if writing for subdirectories, you would enter /subdirectory
RewriteBase /
#Removes access to CodeIgniter 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]
#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
#This last condition enables access to the images and css
#folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
In the CI config file:
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
Source: codeigniter.com/wiki/Godaddy_Installaton_Tips
It's an .htaccess issue. Try changing your .htaccess file to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
Your updated .htaccess is right. you need a small change in it.
change below line in your .htaccess file:
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
and in your config.php
$config['uri_protocol'] = 'QUERY_STRING';
This is not a godaddy solution, but same error on my WAMP localhost...
I had a simple fix to this, which started to occur after I upgraded my WAMPserver. It had set up a new apache config and it had turned off the rewrite_module.
To fix, I just left click wamperver icon->Apache->Apache Modules->rewrite_module and make sure that is ticked.

Resources