Remove “index.php” from URL - Codeigniter

2019-01-12 00:44发布

I've looked in the documentation of Codeigniter of removing the index.php from the URL when accessing different views, the code shows how to remove it with apache:

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

However, when I go to http://localhost/code/home, I get this:

The requested URL /code/home was not found on this server.

But accessing http://localhost/code/index.php/home works just fine, how come it isn't working for me?

I'm on a Mac OS X Snow Leopard using the Sites directory: /Users/~myusername~/Sites/code, and I'm not using any software, i.e. MAMP, XAMPP.

EDIT

For the sudo /usr/sbin/apachectl -k restart -S command, I get this:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server code.local (/private/etc/apache2/httpd.conf:146)
         port 80 namevhost code.local (/private/etc/apache2/httpd.conf:146)
Syntax OK

12条回答
走好不送
2楼-- · 2019-01-12 00:58

To remove index.php from URL just add

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

in the .htaccess file and move the .htaccess file near to index.php location

/var/www/CodeIgniter/index.php
/var/www/CodeIgniter/.htaccess

in config.php in config folder set

$config['index_page'] = '';
查看更多
成全新的幸福
3楼-- · 2019-01-12 00:58

You should change

RewriteRule ^(.*)$ /index.php/$1 [L]

to

RewriteRule ^(.*)$ /code/index.php/$1 [L]

You also need to modify the $config['index_page'] variable like this:

$config['index_page'] = '';

Please refer to my post at http://www.boxoft.net/2011/07/removing-index-php-from-codeigniter-2-0-2-url/ if you like.

查看更多
时光不老,我们不散
4楼-- · 2019-01-12 01:02

in /etc/apache2/users/.conf

try adding ..

<Directory "/Users/<your user name>/Sites/">
    Options Indexes MultiViews FollowSymlinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>

and restart Apache to check results ..

查看更多
Viruses.
5楼-- · 2019-01-12 01:02

You all uri parts are rewrited, so it points to /index.php/code/home. The and good way to do it is to setup mapping eg. http://code.local to point to your /Users/~myusername~/Sites/code, then all your requests are resolved on the root level and everything will work as it should.

EDIT: Downvoters seem to be ignorants, but nevermind..

Here's what your code does now:

GET http://localhost/code/home
// rewritten to:
http://localhost/index.php/code/home

So like I said before the GOOD way would be host mapping so you get everything on the root level (which is GOOD as most likely you will have it this way on the production serv...) and that will do the trick withour changes in .htaccess.

Otherwise you may use Francesco's advice with RewriteBase, but a bit changed:

// Francesco's RewriteRule does this:
http://localhost/code/home
// rewritten to"
http://localhost/code/index.php/code/home

so simply change the RewriteRule to this:

RewriteRule ^code/(.*)$ index.php?/$1 [L]
// rewrites in your case to:
http://localhost/code/index.php/home
// which is what you wanted, aye?
查看更多
姐就是有狂的资本
6楼-- · 2019-01-12 01:07

copy the .htaccess on your main codeigniter folder, mine was localhost/codeigniter/ and then change the {RewriteBase / to RewriteBase /codeigniter/}

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-12 01:08

Try this one

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA] 

I tried 3 before I got one to work

查看更多
登录 后发表回答