CodeIgniter Mod_Rewrite issues - .htaccess

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'] = '';

Related

.htaccess issue | Codeigniter

I'm relatively new to Codeigniter and MVC. But, have successfully made two apps 'Locally'. While exploring, I found a way to remove 'Index.php' from the URL and also about custom routes. The .htaccess file that i have works like charm locally, but when trying to host it; there is a issue 505 internal server issue
Here is the first .htaccess code that i have (works locally) :-
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When i use the code above there is an error. And, the app works only when .htaccess is removed. (I then have to use the inconvenient long URLs)
After a brief research and using different .htaccess without success, i asked one of my friends who has a Hosted CI app successfully running. He sends me a file which leads me the landing page without any problem; but, cant call any functions with/without using routing .i.e. If i use the custom routed URl (www.mySite.com/contact) then also it leads me to the landing page, the same with actual URL scheme (www.mySite.com/welcome/contact_page)
The new code here:-
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Using this code shows me the landing page but i cannot navigate any further from there. When i try to call a function the landing page reloads. This, I think is because of the last error Handling Code (ErrorDocument 404 /index.php)
Does anyone know what the solution the problem might be??
questions
Why doesnt the first .htaccess code work when hosted?
What may be the issue with the second available .htaccess file??
Do you guys have better .htaccess file? If yes, can you post it here??
Try this .htaccess (Codeigniter Recommended )
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In application/config/config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty
And make sure Controller name, Model names are in proper way. Bcz Linux Host is an Case-Sensitive.

URL Redirect through .Htaccess doesn't work in Php Codeigniter

I want to that when I enter localhost/projectname/index.html it will redirect to
http://localhost/service/welcome/index
For this I write that in .htaccess but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Redirect /index.html http://localhost/service/welcome/index
How can I do that?
In codeigniter there is no files are calling in URL. All what Codeigniter do is use MVC Structure to render view.
As well as if you access anything on root folder(index.html or index.php) it will redirect to application folder. Then default controller is Trigger out.
cz in root there is file call index.php, there you can see
$application_folder = 'application';
$system_path = 'system';
And you can take look at this
routes - config/routes.php
controller - controller/
model - model/
view - view/
and we use .htaccess to remove index.php in URL.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
if you want to add .html or .php end of URL
in config/config.php
/*
|--------------------------------------------------------------------------
| URL suffix
|--------------------------------------------------------------------------
|
| This option allows you to add a suffix to all URLs generated by CodeIgniter.
| For more information please see the user guide:
|
| http://codeigniter.com/user_guide/general/urls.html
*/
$config['url_suffix'] = '';

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.

codeigniter and htaccess

I recently moved a functional codeigniter application to a new hosting provider and am running into what I think are challenges similar to this topic. To summarize, my $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO'] values are not being set unless I include the index.php reference in the URL. I'm assuming this means that my .htaccess may be the culprit or a combination of issues. Here's what I've done:
I edited codeigniter's config.php file to remove the index.php reference
$config['index_page'] = '';
I created the following .htaccess file to remove the index.php from the URL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I've toggled between the RewriteRule [L] and [QSA,L] values and get the same behavior.
In the config.php file, I set the following value
$config['uri_protocol'] = 'PATH_INFO';
Using this configuration above, attempting to open www.mysite.com/test/1234 simply opens www.mysite.com. But when I open www.mysite.com/index.php/test/1234, the $_SERVER[PATH_INFO] => /test/1234, but only opens www.mysite.com.
When I switch back to:
$config['uri_protocol'] = 'AUTO';
I can access www.mysite.com/test/1234 (without needing /index.php/), but the $_SERVER[PATH_INFO] value is not being set.
Any suggestions are appreciated.
There is no reference to PHP version in development and production environment, but at first glance I would try changing the cgi.fix-pathinfo setting:
http://php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
if php.ini editting is available.

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.

Resources