Remove index.php from url in codeigniter using htaccess - php

I tried to remove index.php from my url in codeigniter i tried by creating with .htaccess file but it does not works.
RewriteEngine on
RewriteBase /
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
I tried by using this code and also these are mu bse and site url.I not have much knowledge that how to use htaccess properly.Any help is appreciable.
$config['base_url'] = 'http://localhost/xxxx/';
$config['site_url'] = 'http://localhost/xxxx/index.php';

Open config.php and do following replaces
From:
$config['index_page'] = "index.php";
To:
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
Add in .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Hope this will help you :
Options +FollowSymlinks -Indexes
RewriteEngine on
DirectoryIndex index.php
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and also set your config.php:
$config['index_page'] = '';
$config['base_url'] = 'http://localhost/foldername/';
for more : https://www.codeigniter.com/user_guide/general/urls.html

Try the below code to remove index.php from url, Make an htaccess file in the root folder and paste the below code in it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
For remove index.php in the url you need to change the configuration.
Change the following in your config.php file
$config['index_page'] = "index.php";
TO
$config['index_page'] = "";
This will remove index.php from url.

If it helps anyone here, this problem drove me crazy and the solution for me was removing the capital letters from the project folder (it was originally localhost/working/CourseIgnite/).
so after many attempts at the problem I changed site to localhost/working/courseignite/
Then using the solutions that everyone posts worked. Mod rewrite on, change base url and index in the config, and used the below in the htaccess.
Just hope this helps as it was driving me bonkers.
RewriteEngine on
RewriteBase /working/courseignite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Then in application/config
$config['index_page'] = "index.php";
TO
$config['index_page'] = "";
and also in application/config
$config['base_url'] = "yourlocalhostip/working/courseignite/";

Related

Is it possible that ci url will not work with index.php

Is it possible that ci url will not work with index.php.I dont want to add index.php in url. If someone adds index.php in url it should not work.I have tried many methods . Please give me suggestion.
I have tried but it's not working
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
for e.g
if i use below url This should work..............
`example.com/abc`
if i use below url shouldn't work
`example.com/index.php/abc`
Create .htaccess file in your root directory And keep this code into it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Open application/config/config.php
//find the below code
$config['index_page'] = "index.php"
//replace with the below code
$config['index_page'] = ""
/find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI"
You can use:
RewriteEngine On
# 403 Forbidden for index.php
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^ - [F]
# Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
In Config.php
$config['base_url'] = 'http://localhost/website/';
$config['index_page'] = '';
# If Live site
# $config['base_url'] = 'http://stackoverflow.com/';
In .htaccess
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]
Check my answer on another question Set up the base URL in codeigniter
Try the following code than..
change the following in config.php
$config['index_page'] = ""
$config['uri_protocol'] = "REQUEST_URI"
Now open .htaccess file and replace with below code..
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
if above index line do not work the replace the line with new code. Because .htaccess depend on server
// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Add below code in the top of the index.php. I think this will solve your problem.
if(strpos($_SERVER['REQUEST_URI'],'index.php') !== false){
header('location: http://xyx.com/404');
die();
}
Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,END]
RewriteRule ^index\.php* - [F]
What exactly do you mean by saying url shouldn't work should it redirect to another or be replaced?
url shouldn't work - i think you must say to a user (to a crawler) that there is an error in this link, if the link is already indexed, tell crawler that this is 404 not found page, and it will stop indexing that pages. You can't disable index.php/foo totally, because all requests are going through index.php (i believe, if you are using routing)
Here is replacement example - https://htaccess.madewithlove.be?share=04dcf45c-32e9-5c29-b706-5869cd369ffd
Input url http://ex.com/index.php/abc
RewriteEngine On
RewriteRule index.php/(.*) /404 [L] #send user to not found page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Output url http://ex.com/404
Just change your root .htaccess file:
RewriteEngine on
RewriteBase /projectname
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Change in config file:
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

Removing index.php from CodeIgniter URL

I'm struggling with removing the 'index.php' part of my CodeIgniter URL.
I'm using CodeIgniter version 3.1.2. I've followed all steps on other questions about this:
Changed $config['index_page'] = 'index.php' to $config['index_page'] = ''
Changed $config['uri_protocol'] = 'AUTO' to $config['uri_protocol'] = 'REQUEST_URI'
Added a .htaccess file in the root folder of my project with the following code:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
However when I try to access my site, I get the following error code.
Not Found
The requested URL /Toneel/Site/index was not found on this server.
The rewrite module is turned on in my Apache Server.
Can anyone help me out with this issue?
This setting worked for me.
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
To allow overriding htaccess in Apache Configuration you have to edit and change /apache2.conf file to
AllowOverride All
And restart server.
Try this code in .htaacess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Try this in your .htaccess file
RewriteEngine On
RewriteBase /yourfolderhere/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
In my case, it works with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project-name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
** Important, this should be in an .htaccess file in the root of your project.
In config should be like this:
$config['base_url'] = 'http://your-url-here/';
$config['index_page'] = '';

Codeigniter remove /index.php from URL

Okay so I know that a lot of people have been asking the same or similar questions, but I don't seem to find a solution that works for my project. So please don't put this as a duplicate.
I have a website made with codeigniter. It was running just fine without the index.php in the url. So the rewrite was successfull. Until a few days ago: all of a sudden the admin area (/admin) returned a 404 and was only accessible by the path '/index.php/admin'. The rewrite code that once used to work didn't do a thing anymore.
And know I'm back at the start -> the problem is that I cannot delete the index.php from my url.
I tried so many things that I don't know what to do anymore.
The application is stored under 'public_html/application/...
The image below is inside the application folder where the .htaccess file and index.php are.
Is this the right location to change the .htaccess?
I tried rewriting the .htaccess but without success:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^CI/(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots|css\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
My config file:
$config['base_url'] = 'http://www.mysite.be/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
Routes.php:
$route['default_controller'] = "c_home/index";
$route['404_override'] = '';
$route['admin'] = "c_home/adminpanel";
$route['logout'] = "c_login/logout";
Does anybody have a clue what is going on or how this can be fixed?
Thanks in advance!
The .htaccess should be in the public_html directory -- ie: one level up from the current location.
The index.php is presumably in public_html already and
The index.* you have in the picture is index.html -- as it should be.
If .htaccess is in the right place per the above, then this .htaccess code will work:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
So I have been messing around with this and finally made it work
For this I made changes to the htaccess and controllers.
I changed the admin function to a new controller and changed this in the routes.php
new routing
$route['admin'] = "c_login/index";
new .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Try set
$config['uri_protocol'] = 'PATH_INFO'
And set
$config['enable_query_strings'] = FALSE;
If we need a remove /index.php from url in codeigniter
Than
application/config/config.php
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
add .htaccess file at root directory of project
RewriteEngine on
RewriteBase /[Your project folder name]/
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Now you can access like
http://example.com/controller/method
May it's helpfull for you
I don't know what the heck you're doing with all that code, but I just put this in my .htaccess file:
RewriteRule ^(.*)$ /index.php?/$1 [L]
And boom.

how to remove index.php in .htaccess file in codeiginator

Hi I am a newbie at codeigniter.
When i access the url name "http://abc/php/newseller/home/sign" it is giving 404 error. But when i access http://abc/php/newseller/index.php/home/sign then it will give the page. I want to remove "index.php" and my site should run with this url "http://localhost/php/newseller/home/sign"
For this i tried in .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And also remove index.php in application/config/config.php
$config['index_page'] = '';
but getting the same problem of 404
you can try to change also this method in config.php
$config['uri_protocol'] = 'AUTO';
Change the setting in the config.php file
$config['index_page'] = 'index.php';
$config['index_page'] = '';
please remove an index.php file and the set the base URL it will work fine.
please try this htaccess code.
RewriteEngine On
RewriteBase /Project Folder Name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
#RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Removing index.php of codeigniter on local

I'm trying to remove index.php,in my localhost, but it seems doesn't working,its on http://localhost/testing.
I put .htacces in 'testing' directory under the htdocs.
LoadModule rewrite_module modules/mod_rewrite.so
at Apache/conf also already unchecked.
Here my .htaccess:
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /testing/index.php/$1 [L]
Here my config:
$config['base_url'] = "http://localhost/testing";
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
When I access login controller, it's not found.
Not Found
The requested URL /testing/login was not found on this server.
I really don't know what to try next. Any help would be appreciated.
Try this :-
Config.php :-
$config['base_url'] = 'http://localhost/testing/';
$config['index_page'] = '';
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your htaccess should be like
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
There's no need to append /testing/ since RewriteBase already takes care of that.
basically the index.php is included in all URLs:
we can remove this using .htaccess with a simple rules. following is an example of such a file, using the "negative" method in which everything is redirected,
RewriteEngine on
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
now restart your server and check
Leave out the trailing slash in RewriteBase:
RewriteEngine On
RewriteBase /testing
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] should be http://localhost/testing/
$config['index_page'] should be blank.
Try this one in the .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
ErrorDocument 404 /mainpage
your config file is fine
Difference being the "?" after index.php
You may also want to try changing the value of $config[‘uri_protocol’] in ./application/config/config.php. Sometimes CodeIgniter gets it wrong. Changing it to PATH_INFOmight work.

Categories