Codeigniter's URL Rewriting - php

I'm using the following htaccess script so that I can hide index.php from the URI.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
But I'm facing a major problem :(
I have a directory named assets beside my index.php file and it should be there. When I browse the directory by browser, then Codeigniter’s not found page displays. I can't browse the file /assets/image.jpg but it displays when I call it from an <img> tag
What can I do now?
Note that it is working in my local server (localhost) but not in the live server.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]
That'll pass any request that Apache wouldn't serve off to CodeIgniter. This way you're free to create directories all over without updating your rewrite rules, and you don't need to setup 404 handling in Apache (even inside your image/resource directories).
Credit goes to the Zend Framework authors that recommend this bit in their user guide. This version is slightly modified for CodeIgniter.

This is the .htaccess file I use for my CI install:
If you have it in a subfolder you will need to edit RewriteBase.
<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
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>

Here is complete solution which i am using and found working nicely:
Place this code snippet below in .htaccess file at your project root like http://localhost/myProject
RewriteEngine On
RewriteBase /appDirName
RewriteCond %{REQUEST_URI} ^appDirName.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And at your CI config file located at system\application\config\config.php
$config['uri_protocol'] = "REQUEST_URI";

# Turn on URL rewriting
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the images/, js/, or css/ directories
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|images|js|css)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
I use this and it work perfectly!

Insert the following into your .htaccess
# Change "welcome" to your default controller:
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/$ $1 [L,R=301]
# Checks to see if the user is attempting to access a valid file,
# if not send them to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

htaccess on codeigniter in the website

Hi i have problems in the url .. i think the htaccess code when i uploaded my file to the live website i notice that the error is thi -------------->No input file specified. here is my link to the website http://springrainglobal.com/movies_world/index.php/movies
here is my htaccess code
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|images|js|assets|css)
RewriteRule ^(.*)$ /movies_world/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /movies_world/index.php?/$1 [L]
can anyone help me out on how to configure properly the htacces on live server?pls help
Your site is available under http://springrainglobal.com/movies_world/movies (leave the index.php), if that's what you're looking for.
This is the standard .htaccess I always use for CodeIgniter builds:
<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
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>
If you change line 3 of that code to
RewriteBase /movies_world/
you should be set.

My CodeIgniter project not running on WAMP server

I have my project developed in CodeIgniter which works perfect on XAMPP Server but Not working on WAMP server.
I've changed
$config['index_page'] = 'index.php'; to $config['index_page'] = '';
and .htaccess I've
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Homepage of website appears properly. But no link is working whenever I click on any link wamp server homepage appears in French version.
Project run properly on WAMP only if there is index.php in URL.
e.g.
http://localhost/codeigniter/welcome/view (This link doesn't work)
http://localhost/codeigniter/index.php/welcome/view (Links works properly)
Try this one, it's working everywhere :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
Enable the mod_rewrite in your httpd.conf file
In my case I have installed WAMP on local disk C
to find httpd file just navigate to C:\wamp\bin\apache\apache2.2.6\conf
open httpd.conf file with notepad editor then find the line below:
#LoadModule rewrite_module modules/mod_rewrite.so
and uncomment it
save the file and restart Apache for the new setting to take effect.
try this
RewriteEngine On //missed this
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
check RewriteBase in .htaccess file.
it would be like below
RewriteBase /your project folder name/
copy below code and paste it in your .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymlinks
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]
#RewriteRule ^page/(.*)$ index.php?/#$1 [R=301,L,NE]
# 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;
hope it will help you. and check one thing i.e your url. if there is any ~ symbol then .htaccess file will be different. otherwise the above code will work. Thanks.

Having trouble removing index.php from CodeIgniter

So far I have a fresh install of 2.1.3 and have placed an .htaccess file with:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
in the root directory, same level as application, system, etc.
Edited $config['index_page'] = 'index.php'; to
$config['index_page'] = ''; in application\config\config.php.
Turned on rewrite_module.
Restarted Apache.
But I still end up with a 404 even after trying all the solution, even those in related posts.
Hope this could help this is my htaccess and it works for me.
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>
<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
You need to modify your httpd.conf to enable mod_rewrite and make sure that your webroot directory has AllowOverride set to 'ALL'
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
use this it will work

CodeIgniter mod_rewrite gives 404 error

I have written a CodeIgniter application and I want to use mod_rewrite to clean up my URL's. I have installed the CodeIgniter application in htdocs/ci-intro/
Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci_intro/
#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>
I altered the config.php in CodeIgniter to the following:
$config['base_url'] = '';
$config['index_page'] = '';
Whenever I use my links to go to the about page I get a 404 message from CodeIgniter. The URL in my browser seems to be 'cleaned up' though (index.php is removed). This is the URL:
'http://localhost:8888/ci_intro/about'
Can anybody help me getting rid of the 404 error and actually directing the use to the about page?
OS: MAC OS X 10.8.2 (latest)
SERVER SOFTWARE: MAMP
PHP: 5.2.17
BROWSERS: Chrome & Safari (In both browsers this problem occurs)
Before reviewing your server configration, please try code below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci_intro/
#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.*
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>
try this...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /Your_folder_Name/index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /Your_folder_Name/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Your_folder_Name/index.php?/$1 [L]
</IfModule>
Coincidentally I am using the same configuration like yours, and end up it didn't really work no matter how i change it.
So I gave up changing it and tried another simplest setting.
Try this out, it works for me!! :)
<IfModule mod_rewrite.c>
RewriteEngine on
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|images|uploads)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^system.*
probably isn't going to trigger, since REQUEST_URI starts with /. Try
RewriteCond %{REQUEST_URI} ^/system
Ditto for
RewriteCond %{REQUEST_URI} ^application.*
And does your system really want /index.php?/blah blah? It's going to expect a URL Query String after the ?.

Codeigniter htaccess and base_url

I install codeigniter and start to write some code on it.First i want to remove index.php and make some research about it.I remove it with a small htaccess code below,
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /pasaj/index.php/$1 [L]
and then set my base_url in config.php as,
$config['base_url'] = 'http://localhost/pasaj/';
and then i decided to use base_url in form's actions
and wrote it like that(please look at action)
<form method="post" id="formSubmit" name="fSubmit" action=<?php base_url(); ?>"kayit/kayitOnay/">
but it did not work
and prints url like that
what i did all is not working
and i suspect from my htaccess file.I removed it. i use it for removing index.php in codeigniter default.
and set my new
base url like that necessarily
http://localhost/pasaj/index.php/
and do not change anything above form code
and it works.
Now, i want to still use this htaccess file in order to remove index.php but when htaccess file exists my base_url works wrongly . What might be the changes in htaccess file ?
try this
RewriteEngine on
RewriteBase /pasaj/
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
My htaccess files on a local server looks like this:
For a Zend Framework web:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
For a CodeIgniter web:
RewriteEngine on
RewriteBase /pasaj/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
This is my standard htaccess file.
<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
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>
You may need to alter the rewrite base.
To remove index.php from url you can try the following.
Open config.php from system/application/config directory and replace
$config['index_page'] = “index.php” by $config['index_page'] = “”
Create a “.htaccess” file in the root of CodeIgniter directory and add the following lines.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In some case the default setting for uri_protocol does not work properly. To solve this problem just replace
$config['uri_protocol'] = “AUTO” by $config['uri_protocol'] = “REQUEST_URI” from system/application/config/config.php

Categories