Removing index.php in CodeIgniter using .htaccess file - .htaccess

I'm currently developing a website using CodeIgniter and I recently stumbled upon a routing & .htaccess problem.
Basically, the structure of my simple website (let's call my project 'CIExample' for the sake of simplicity) is as follow:
-Home
-About Us
-Our Service
-News
-Contact Us
which I implemented using a Page controller. This controller has 5 functions which called respective page's view, i.e:
Home(), which calls the view 'Home'
About(), which calls the view 'About Us' and so on..
Previously, to access 'Home', I would need to type http://localhost/CIExample/index.php/page/home into the url window, but after setting the following routes I was able to remove the 'page' (classname) part:
$route['default_controller'] = 'page/home';
$route['home'] = 'page/home';
$route['about'] = 'page/about';
$route['service'] = 'page/service';
$route['news'] = 'page/news';
$route['contact'] = 'page/contact';
However, the tricky part came when I try to remove the 'index.php'.
I want to be able to access the home page by typing http://localhost/CIExample/home.
So I did a lot of searches on CI forum/tutorial/stack overflow, and found some codes for .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
or http://ellislab.com/forums/viewthread/197675/#929904
I tried both codes but none works..
http://localhost/CIExample/home would direct me to 404 not found page, but http://localhost/CIExample/index.php/home would work just fine.
I wonder what went wrongs? Is it my routes.php or .htaccess or both?
Thanks.
Note: I've also changed my 'config/config.php' file -> $config['index_page'] = '';
EDIT:
Finally it works!
After tweaking the config.php file in config folder and set the following $config['uri_protocol'] = 'PATH_INFO'; (from 'AUTO').
Available Values:
| '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
Dunno what's the different but according to one of the comments in http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/, the same code might/might not work in the production server.
Anyone can explain the reason maybe?

You just need to paste this in your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
and make this alternation to your application/config/config.php file:
$config['index_page'] = '';

I have use following method, it is working perfectly in UBUNTU...!!
First of all verify that you have enabled mod_rewrite (you can verify using phpinfo()) in your index.php file.
IF DISABLED mod_rewrite then RUN:
1. sudo a2enmod rewrite
2. sudo gedit /etc/apache2/sites-available/defaut
just replace "AllowOverride None" to "AllowOverride All" in Tag(<'Directory /var/www/'>)
3. Then please run sudo service apache2 restart
Ok Good...
Now in your root directory of Codeigniter -> Find .htaccess file, if not found then press ctrl+h , still not found then create a file .htaccess
In .htaccess
Paste following code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ROOT DIRECTORY NAME/
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Ok finally just replace ROOT DIRECTORY NAME
DONT FORGOT TO REMOVE index.php from application/config/config.php file
Make that line like
$config['index_page'] = '';

I use this htaccess for all my CodeIgniter projects. It supports subfolders too.
<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>

In your system/application/config/config.php, Change
$config['index_page'] = "index.php";
To
$config['index_page'] = "";
And in your .htaccess,add this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

for Codeigniter 3.1.4
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php5_module>
php_flag asp_tags On
php_flag display_errors On
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 512M
php_value post_max_size 128M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_value upload_max_filesize 128M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Live:
#RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Local:
#RewriteRule .* index.php/$0 [PT,L]

Related

Error installing another PHP app in subfolder of SilverStripe

I have Silverstripe installed on www.mywebsite.com (I made that up just as an example)
I am testing Wordpress on www.mywebsite.com/test
Here is my .htaccess file in my root folder:
### SILVERSTRIPE START ###
# Deny access to templates (but allow from localhost)
< Files *.ss >
Order deny,allow<br>
Deny from all<br>
Allow from 127.0.0.1
< /Files >
# Deny access to IIS configuration
< Files web.config >
Order deny,allow
Deny from all
< /Files >
# Deny access to YAML configuration files which might include sensitive information
< Files ~ "\.ya?ml$">
Order allow,deny
Deny from all
< /Files>
# Route errors to static pages automatically generated by SilverStripe
ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html
< IfModule mod_env.c>
# Ensure that X-Forwarded-Host is only allowed to determine the request
# hostname for servers ips defined by SS_TRUSTED_PROXY_IPS in your _ss_environment.php
# Note that in a future release this setting will be always on.
SetEnv BlockUntrustedIPs true
< /IfModule>
< IfModule mod_rewrite.c>
# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
< IfModule mod_dir.c>
DirectoryIndex disabled
< /IfModule>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine Off
# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]
# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]
< /IfModule><br>
### SILVERSTRIPE END ###
< IfModule pagespeed_module>
ModPagespeed Off
</IfModule>
< IfModule mod_expires.c><br>
ExpiresActive Off<br>
< /IfModule>
Code on /test/.htaccess:
< IfModule mod_rewrite.c><br>
SetEnv HTTP_MOD_REWRITE On<br>
RewriteEngine On<br>
RewriteBase /<br>
RewriteCond %{REQUEST_URI} !/test<br>
RewriteEngine On<br>
RewriteCond %{REQUEST_FILENAME} !-f<br>
RewriteCond %{REQUEST_FILENAME} !-d<br>
RewriteRule . /index.php [L]<br>
< /IfModule>
Any ideas? Note: The < br > does not show up in the actual file and there are no spaces between the < and >
I get a list of all my files. My index.php file doesn't even show up properly and the URL changes from /test to test/?url=/test
I use zoom.ph shared hosting.
Update:
My new code in the root .htacccess file:
RewriteEngine On
# Change any direct URLs (www.unclebubby.com...) to the subdomain (wavs.unclebubby.com)
RewriteCond %{HTTP_HOST} ^(www\.)?collegeconnect\.ph$ [NC]
RewriteCond %{REQUEST_URI} ^/test/(.*)$ [NC]
RewriteRule .* http://test.collegeconnect.ph/%1 [R=301,L]
RewriteCond %{REQUEST_URI} !/test
# If there is a .htm at the end of the URL, get rid of it (due to migration of site from FrontPage)
RewriteCond %{REQUEST_URI} ^/(.+)\.htm$ [NC]
RewriteRule ^.+\.htm$ /%1 [R=301,L]
RewriteBase /
### SILVERSTRIPE START ###
I tested this and what you have seems to work fine. Just ensure you have no errors in your htaccess. You can check apache.log for this.
Here's what I used:
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/test
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This redirected me successfully to the Wordpress setup page (fresh install).
First, your RewriteBase in test/.htaccess is wrong. It's not "/" (root), but "/test/" (as files are there).
Second, you have "RewriteEngine On" twice in your test/.htaccess - which may not be an error, but not needed anyway.
Third - your test/.htaccess file is literally disabled by itself by line:
RewriteCond %{REQUEST_URI} !/test
which means - all requests that don't have "test" in URL, which no request to "test" folder can fullfil. Your test/.htaccess should look like this:
< IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
< /IfModule>
BONUS: Silverstripe scans entire folder where it is installed and adds those classes to its manifest, which means you should exclude wordpress (ie folder "test") by placing empty file called "_manifest_exclude" in it.
mention folder name in 'RewriteBase' like below in .htaccess file at your root path
RewriteBase '/SilverStripe/'
where SilverStripe be ur folder name like u mentioned
RewriteBase '/test/'
please do remember to add slash before and after the folder name. If it doesnt work , then let me know. Thanks

CodeIgniter controllers are not loading and giving a 404 error

I am testing gocart (http://gocartdv.com/) and I have installed it in a subfolder called vine-cart, http://www.icas-v.org/vine-cart.
The default controller loads fine but the rest of the controllers return an error 404 message. I have looked through all the similar problems and the possible solutions, implemented them and they don't seem to work.
Please have a look at my configuration settings:
/---------------------------------|.htaccess file settings|---------------------------------/
RewriteEngine On
RewriteBase /vine-cart
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(cart(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
###
# 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]
# 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>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
/---------------------------------|Config.php settings|---------------------------------/
$config['base_url'] = 'http://www.icas-v.org/vine-cart/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
/---------------------------------|routes file settings|---------------------------------/
$route['default_controller'] = "cart";
//this for the admininstration console
$route['admin'] = 'admin/dashboard';
$route['admin/media/(:any)'] = 'admin/media/$1';
Ive been trying to debug this for days. Please help.
Can you try this htaccess code? remove rewritebase.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vine-cart/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vine-cart/ [L]
</IfModule>
What did you name your controller file?
Also it's a good practice to comment out lines on your .htaccess as you test them 1 by 1 if its causing issues.

2 codeigniter applications & htaccess

I had a codeigniter proyect working in windows with XAMPP and now I was moving it to my RaspberryPi:
Apache/2.2.22 (Debian)
PHP 5.4.4-14+deb7u5
mysql Ver 14.14 Distrib 5.5.31
I have two codeignitir aplications (principal and panel) with this estructure
/ -> (apache root folder)
/appname/admin
/admin/application/ -> (codeigniter application to panel)
/appname/application/ -> (codeigniter application to public)
Now I can't access to admin controllers and I think is because .htaccess. I get this in some url:
my.domain/appname -> codeigniter welcome for public (this part is empty so I think is OK)
my.domain/appname/admin/login.html -> Not Found. The requested URL /mydomain/admin/login was not found on this server.
my.domain/appname/admin/index.php?controller=admin -> 404 Page Not Found (codeigniter style)
my.domain/appname/admin/hello.html -> hello (a dummy file I put to try, works ok)
In my codeigniter panel proyect I have
$config['base_url'] = 'http://my.domain/myapp/admin/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '.html';
I think it's all about .htaccess but I don't understand them. How many files I need? What folders?
I had this .htaccess in my /appname & /appname/panel before
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /appname #to panel /appname/panel
#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]
#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/$0 [PT,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>
I think RewriteEngine on is activated in my apache2.
I found solution by .htaccess inside admin's folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /appname/admin
#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 'appname' to your applications folder name.
RewriteCond %{REQUEST_URI} ^appname.*
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>
CodeIgniter multiple applications htaccess
<IfModule authz_core_module>
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

CodeIgniter Mod_Rewrite issues

Having issues removing index.php? from the URL:
http://localhost/index.php?/reset_password
With htaccess file:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
CI Settings:
$config['uri_protocol'] = 'AUTO';
I looked at the other threads on here with similar problems but none of their htaccess files work. Am I missing a setting?
I've never seen that .htaccess setup before, although it may work. I've used this for every CI project I've done, give it a try:
<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|themes|uploads)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Add don't forget this in config.php:
/*
|--------------------------------------------------------------------------
| 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'] = '';
And remember: This will not "remove" index.php from your URL automatically, but rather route the request through it if it isn't present. If you have hardcoded links with index.php in them, they'll have to be changed or you'll need additional .htaccess configuration. If you are using the CI url functions like base_url(), site_url(), and anchor(), they won't use index.php if you have it blank in your config above.
This code works for me:
If name of my site is codeIgniter_ci_intro
I used code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeIgniter_ci_intro//index.php/$1 [L]
and I changed in config.php
$config['index_page'] = '';

mod_rewrite, can't rewrite existing file

I'm trying to pass all images in a directory to watermark.php using mod_rewrite in .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
It works fine on local machine, but on my online production server (shared hosting) all image files are served without rewriting.
mod_rewrite is enabled online, but it ignores the rule if file exists.
What could be wrong?
UPDATE
Here's my full setup: there's a domain with a subdomain in a subfolder of main domain's document_root.
public_html (example.com DOCUMENT_ROOT)/
img (img.example.com DOCUMENT_ROOT)/
.htaccess in public_html folder:
<FilesMatch "\.(inc\.php|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file /home/username/public_html/bootstrap.inc.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^img\. [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ shop/$1 [L]
.htaccess in img folder:
<FilesMatch "\.(inc|log)$">
Deny from all
</FilesMatch>
Options -Indexes +FollowSymLinks
php_value short_open_tag 0
php_value auto_prepend_file none
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.jpg$ /watermark.php?path=%{REQUEST_FILENAME} [L]
The problem is that existing images in img folder are not rewrited. Apache just serve them as is. But if, for example, I request non-existing file the rule works and echo $_GET['path'] prints the full filepath to non-existing file.
Here's what you can use:
RewriteEngine On
RewriteRule ^(media|image|images)(/?)(.*)$ - [F]
RewriteRule ^([a-z0-9\-_\.]+)\.(jpg|jpeg|png|gif)$ /watermark.php?path=%{REQUEST_URI} [L]
The first RewriteRule is set to make sure people don't go to that folder - it gives a 403 permission denied (this is optional)
The second one will redirect any images that match letters, numbers, dash, underscore and dot with the extension: jpg, jpeg, png and gif to your watermark.php file. eg:
http://www.domain.com/logo.jpg => path = /logo.jpg
UPDATE:
Since the rule is in a different folder and the root directory of IMG is /home/username/public_html/img/ the watermark.php file does not exists. Either copy the file you need (watermark.php and its libraries) to the IMG folder or create symblink.
cd /home/username/public_html/img/
ln -s ../watermark.php (and other library files as well)
htaccess change :
Options -Indexes +FollowSymLinks
by
Options -Indexes +FollowSymLinks -MultiViews
This behaviour steel exists on Apache 2.4..

Resources