jsky
jsky

Reputation: 2217

Why is .htaccess url rewrite not working codeigniter mamp?

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

Upvotes: 1

Views: 9879

Answers (4)

jsky
jsky

Reputation: 2217

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

Upvotes: 5

Nishant
Nishant

Reputation: 3704

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.

Upvotes: 1

Filippo oretti
Filippo oretti

Reputation: 49873

 $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

Upvotes: 2

MJ X
MJ X

Reputation: 9054

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]

Upvotes: 0

Related Questions