1

[edit]: duplicate of How to remove "index.php" in codeigniter's path
[the solutions there dont work for me]

Im trying out codeigniter for the first time and im pretty new to php still. I wanted to remove the index.php from my url.

installed code igniter
replaced index.php that was there with my own

I have this in my .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  


#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>

My rewrite is definately on, i load a phpinfo page in my root and it shows the module loaded. Ive also checked the httpd.conf

when i run from NetBeans i am directed to localhost:8888/domain/index.php and my index page loads
when i go to localhost:8888/domain then my index.php also loads

when i go to localhost:8888/domain/welcome or localhost:8888/domain/welcome.php i get 404
The requested URL /index.php/welcome was not found on this server.
when i go to localhost:8888/domain/index.php/welcome i get directed to the welcome controller but it just loads my index.php but with no markup.

Ive also tried this in the .htaccess:

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [QSA,L] 

and i get:

enter code here

This also didnt work (i know, theyre all meant to be doing pretty much the same thing):

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /
    # 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]

</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 have in config/config.php:

 $config['base_url']    = 'localhost:8888/domain.com/';
    $config['index_page'] = '';
Community
  • 1
  • 1
jsky
  • 2,103
  • 5
  • 34
  • 50
  • 1
    I guess `http:8888/domain/` needs to be `http://domain:8888/` in the `base_url` but I'm not sure. – f605dcf7dc5542e93ae9cd76f Aug 14 '13 at 10:32
  • sorry that was a typeo but it didnt fix the problem: it is now: **$config['base_url'] = 'localhost:8888/domain.com/';** – jsky Aug 14 '13 at 10:37
  • actually i just noticed, going to domain/welcome gives error message: The requested URL /index.php/welcome was not found on this server. – jsky Aug 14 '13 at 10:45
  • why have you replaced index.php with your own? you do not have to remove index.php, you just have to edit the default controller name, and route paths. – Nishant Aug 14 '13 at 11:41
  • yeh i just noticed that too. but these .htaccess hacks arent sending domain/index.php/welcome or domain/index.php/welcome.php to domain/welcome which is what they should be right? oh ok. so theyre only going to work with the routes in place i suppose? – jsky Aug 14 '13 at 12:08
  • @Nishant: i was following a tutorial which told me to. but looking in the default file now looks like it might be code i need. thanks – jsky Aug 14 '13 at 12:19
  • @jsky I have posted a answer giving some details about installing a fresh CI, hope it will help you. – Nishant Aug 15 '13 at 06:17
  • Check this out, hope it helps you http://stackoverflow.com/a/42034605/6435732 – Uchenna Nnodim Feb 03 '17 at 23:30
  • Check this out, i hope it helps you http://stackoverflow.com/a/42034605/6435732 – Uchenna Nnodim Feb 03 '17 at 23:31

4 Answers4

5

I replaced my own index with the default CI index (that is, i returned it to how it was out of the box).
I then routed domain/index.php/pages/view/page.php to domain/index.php/page.php
i then ran Nishant's .htaccess code and although it didn't appear to work (a static non-style version of the site displayed at the usual url but not at the url without the index.php) it may have helped get me to a solution.
i also changed RewriteEngine On to RewriteEngine on though im pretty sure i had tried that before. After these steps i then flicked back over to the code that sbaaaang suggested (which was somewhat a version id already unsuccessfully tried previously) and this time it worked.

For anyone else who might run into .htaccess trouble i would suggest leaving things as they come out of the box and going through the tutes (instead of jumping into it like i did).

My .htaccess ended up like this

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  


<Files "index.php">
AcceptPathInfo On
</Files>  
</IfModule>

<IfModule !mod_rewrite.c>

ErrorDocument 404 /index.php
</IfModule>

and config vars like:

$config['base_url'] = '';
$config['index_page'] = '';
jsky
  • 2,103
  • 5
  • 34
  • 50
2
 $config['base_url']    = 'http://'.$_SERVER['HTTP_HOST'].'/';
 $config['index_page'] = '';

then .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

also i noticed that you said "replaced index.php that was there with my own" this is wrong, leave the original Codeigniter index.php file in the main root or you'll never run the codeigniter :D

itsme
  • 45,343
  • 90
  • 210
  • 334
  • thankyou. i tried this code previous to your answer without success (i quote it in the question). i had also tried with the different base_url and index_page configs. however upon returning to it a couple days later, after running Nishant's code, this code works now for me. Since both your version of the code / which was somewhat already in my question and Nishant's answer led me to the answer i felt the best thing to do was to upvote you both and accept my own answer (detailing what i did)(for which i get no rep for anyhow). – jsky Aug 17 '13 at 04:08
1

Hope this will help you

The problem that your are facing is because you removed the index.php and replaced it with your own index.php.

Instead after putting codeigniter folder at your localhost with index.php at your root of [the local host, open the below url in your browser.

http://localhost:8888/domain/index.php/welcome

if it works fine, means displaying the welcome page of your codeigniter, then you can think of removing the index.php from your url, by adding

RewriteEngine on
RewriteCond $1 !^(index\.php|static)
RewriteRule ^(.*)$ /index.php/$1 [L]

in your .htaccess file, the second line of the rewrite condition will make sure that the rewrite rule will not execute, when there is index.php or static in the url, with static you can put all the static content in the static folder.

now when the url is not having index.php or static in it, rewrite rule will rewrite it.

suppose the url is

http://localhost:8888/domain/welcome

will be rewrited to

http://localhost:8888/domain/index.php/welcome

In every case your code will be served through the url containing index.php only.

Nishant
  • 3,424
  • 1
  • 16
  • 26
  • Thankyou for your answer Nishant, it somehow led me to a working .htaccess index replace. The code that works now is a combination of another code fragment here, and your full code doesnt work for me as is, so i thought the best thing was to vote both codes up and detail what steps i took to make it work and accept that as the answer (i dont get rep). Thankyou for your help. – jsky Aug 17 '13 at 04:18
0

Try this a very simple code it works fine for me:

put the name of your project folder instead of project2 change your config base_url=" "

RewriteEngine on
RewriteCond $1 !^(index\.php|uploads|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /project2/index.php/$1 [L]
MJ X
  • 6,186
  • 8
  • 45
  • 77