Remove index.php in codeigniter 2.1.0
In application/config.php
make sure that
$config['index_page'] = "";
Then set this as your .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're all set!
How to remove “index.php” in codeigniter's in server
Open config.php and do following replaces
$config['index_page'] = "index.php"
Replace With
$config['index_page'] = ""
And
$config['uri_protocol'] = "AUTO"
Replace With
$config['uri_protocol'] = "REQUEST_URI"
HTACCESS
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Codeigniter - removing the index.php
I finally managed to work this out. Thanks for all the answers, special thanks to @ddanurwenda. His rewrite rules were the only ones that solved my problem.
There was an Apache config that I had overlooked: I changed this line (from httpd.conf):
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
To the following:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
That configuration were keeping my htaccess file from being read.
Remove index.php from URL
Hi Sajid I used below code try I may be it will solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /your_folder/
#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>
Removing index.php from codeigniter
I've been playing around with it as well. This is the one which works for me, and I fear touching it: ;) j/k
RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]
It is located outside the application/system/user_guide directories. Resources is my own public folder, ignore that line.
I haven't touched the internal .htaccess files. They're what they were:
Deny from all
Hope this helps :)
Edit: Also check if you have the mod_rewrite enabled..
And this.. Remove index.php in CodeIgniter_2.1.4
Remove and Redirect index.php from CodeIgniter
https://www.example.com/index.php?/my-seo-friendly-uri
This URL contains a query string and so requires a slightly different rule in order to match it. The RewriteRule
pattern matches against the URL-path only (just index.php
in this case). The query string is available in its own variable.
Add the following, before your existing directives (in addition to the directive that matches /index.php/my-seo-friendly-url
- which is passed as path-info):
# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"
# And "/?/my-seo-friendly-uri"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(/.*)
RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]
The query string is captured (2nd condition), and the backreference (%1
) is used to construct the redirect.
The first condition that checks against the REDIRECT_STATUS
environment variable is required in order to prevent a redirect loop, since you appear to be using the query string method to route the codeigniter URLs in the later rewrite. The REDIRECT_STATUS
env var is empty on the initial request, but set to "200" (as in 200 OK HTTP status) after the first successful rewrite.
The QSD
flag (Apache 2.4+) is required to discard the original query string from the redirected request. If you are still on Apache 2.2 then append a ?
(empty query string) to the substitution string instead. ie. %1?
By making the match for index.php
optional (ie. ^(index\.php)?$
) it will also canonicalise URLs that omit index.php
, but still include the query string (that may or may not currently be a problem). eg. /?/my-seo-friendly-uri
.
Note that this is currently a 302 (temporary) redirect (as is your existing redirect). Only change this to a 301 (permanent) redirect once you have confirmed it works OK. 301s are cached persistently by the browser so can make testing problematic.
Summary
Your .htaccess
file should look like this:
RewriteEngine On
# Query string...
# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"
# And "/?/my-seo-friendly-uri"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(/.*)
RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]
# Path-Info...
# Redirect URLs of the form "/index.php/my-seo-friendly-uri"
# Also handles "/index.php" only
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index.php(?:/(.*))?$ /$1 [R=302,L]
# CodeIgniter Front-controller
# (NB: Using query string method to pass the URL)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [L]
Additional notes...
- The
<IfModule>
wrapper is not required. (.*)
is the same as^(.*)$
since regex is greedy by default.- I've modified your existing path-info redirect (ie.
/index.php/foo
) to also redirect requests for/index.php
only. This now requires an additional condition to avoid a redirect loop. Your CodeIgniter front-controller is using the query string method to pass
/my-seo-friendly-url
to the backend (as used in the question). However, you have set$config['uri_protocol'] = 'REQUEST_URI';
- which contradicts with this (although shouldn't necessarily be a problem). However, if you are using theREQUEST_URI
method then you can remove the?/$1
part from the end of the finalRewriteRule
substitution string. For example:For example, from this:
RewriteRule (.*) index.php?/$1 [L]
To this:
RewriteRule . index.php [L]
Related Topics
Composer Killed While Updating
Why Is Textarea Filled with Mysterious White Spaces
Sanitizing User's Data in Get by PHP
How to Trim White Spaces of Array Values in PHP
How to Resolve "Must Be an Instance of String, String Given" Prior to PHP 7
How to Upload and Parse a CSV File in PHP
How to Add a New Method to a PHP Object on the Fly
MySQL Error: 1364 Field 'Display_Name' Doesn't Have Default Value
Print Newline in PHP in Single Quotes
Regular Expression to Check If the String Has Chinese Chars
PHP Create and Save a Txt File to Root Directory
Differencebetween Sessions and Cookies in PHP