Get rid of Index.php in CodeIgniter URLs - php

How do I remove the "index.php" from my URL?
Description
Using CI to build a simple MySQL-Php app, I cannot get rid of the "index.php" tag inside the URL:
localhost/CodeIgniter/index.php/Defult_Controller/index
.htaccess
application/.htaccess and application/cache/.htaccess:
<IfModule mod_rewrite.c>
# !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/
RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
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>
apache.conf
LoadModule rewrite_module modules/mod_rewrite.so
application/config/config.php
$config['base_url'] = "http://localhost/CodeIgniter/";
$config['index_page'] = '';
PS: Im using codeIgniter 2.5.1 and xampp 3.2.1

You need to place your .htaccess in your root folder to remove index.php, application/.htaccess is to avoid directory view.

Related

I don't have access to '/public/assets' using Codeigniter 4 when removing index.php from url using a .htaccess file

I just started using Codeigniter 4 and uploaded the project to a shared hosting. I defined a new sub domain with the local path of /application/public.
I used the default .htaccess to remove index.php from url and it's working but I don't have access to /public/assets. How can I fix this?
This is my .htaccess file:
# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</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>
# Disable server signature start
ServerSignature Off
# Disable server signature end
In /public/.htaccess , change:
RewriteRule ^(.*)$ index.php/$1 [L]
to
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
In app/Config/App.php:
$indexPage = ''; //instead of 'index.php'
$baseUrl ='https://mysubdomain.mydomain.com/'; //slash at the end was important before, not sure now
After that all files and directories in /public/ are actually accessible through web. You may want to limit that with the .htaccess file or just put only stuff that needs to be public.

Codeigniter 3.1.6 - How to remove index.php from url

I am working on codeigniter 3.1.6.
I added the .htaccess file.
I also changed the base_url path to my project path, removed the index.php from index_page and changed the url_protocol to REQUEST_URI.
Still, while I am redirecting the url to any controllers method it throwing an error as 'The page you requested was not found.'
I also searched and applied different .htaccess but its not working.
If I am addling /index.php at end of base_url then its working but its wrong though. It should work without index.php.Only 3.1.6 giving this issue.
note: codeigniter-3.1.4 is working properly only this version is giving an issue
Change folder name CodeIgniter-3.1.6 to ci
Set your base_url to
$config['base_url'] = 'http://localhost/ci/
Use this .htaccess
RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Use this script in .htaccess
<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
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ index.php?/$1 [L]
# If your root folder is at /mypage/test1/
RewriteRule ^(.*?)$ /mypage/test1/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>
Remove index.php from config.php
$config['index_page'] = '';
1) Edit config.php
$config['index_page'] = 'index.php';
To
$config['index_page'] = '’;
2) Create/Edit .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
1:Create .htaccess file in root directory not another project folder directory.
2:write or copy this code and paste it
`<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<IfModule>`
I hope this will work properly.
Go In Config file and make changed
$config['url_protocol']='auto';
Additional tip: make sure .htacces in the root folder not inside >application folder

remove requirement of index.php in url for loading controller- PHP xamp

I am totally new to Code igniter and dealing with form submissions through MVC archi. As, I searched and tried all soultions and code given on stack overflow itself to remove index.php from url but all of them didn't worked. I know something is missing and I am not able to recognize it as I am new to CI.
This is my .htaccess code below,
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have also set $config['index_page']=' '; and $config['uri_protocol']='REQUEST_URI'; in config.php file.
NOTE: My CI version is 3x
Make sure your rewrite base is right like given in screen shot i have given. There are many htaccess files in each folder. I give the htaccess which is in red in the image and the structure also.
<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 /projectfolder //its an example
RewriteBase /project
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 use xampp and windows 10
Place in main directory of project not in the application folder.
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
Structure
application
system
user_guide
.htaccess
index.php
Try some of these also
https://github.com/wolfgang1983/htaccess_for_codeigniter
Don't for get to set base_url
$config['base_url'] = 'http://localhost/yourprojectname/';
$config['index_page'] = '';

Codignator Error File Not found

Hey I have been facing problem in codeignitor "File Not Found" in every controller. I faced in on Project,I fixed it by putting this code in .Htacces
<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>
# 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>
Then again faced this on another project automatically.
Solve This by putting this code in hatccess
Source : https://gist.github.com/philipptempel/4226750
<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>
# 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>

Redirecting in specific address

I use codeigniter for my project. On localhost i dont have any problems with my application. When i put files on server i have a big problem with .htacces i think. I have a address: domain.com/napijmy_sie/index work but address domain.com/napijmy_sie/app doesn't work. Why? I dont have any idea. Here is my .htacces file:
<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 /napijmy_sie
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>
Anyone can help me?
Try this --
<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 /napijmy_sie/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ napijmy_sie/index.php?/$1 [L]
</IfModule>
I hope it should work.

Categories