开发者

Remove "index.php" from URL - Codeigniter

开发者 https://www.devze.com 2023-03-22 09:27 出处:网络
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:

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:

开发者_如何学C

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


You need to make "http://localhost/code" your web root as 'code.local' (in my example). Assuming that your setup on Mac OS X Snow Leopard is the same as mine.

You should add the following lines to "/etc/apache2/extra/httpd-vhosts.conf"

<VirtualHost *:80>
    DocumentRoot "/Users/~myusername~/Sites/code"
    ServerName "code.local"
    ErrorLog "/private/var/log/apache2/code-error_log"
    CustomLog "/private/var/log/apache2/code-access_log" common
</VirtualHost>

Also make sure that it is uncommented in "/etc/apache2/httpd.conf"

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Then you add code.local to your "/etc/hosts" which should look like this

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       code.local

Then restart your apache "/usr/sbin/apachectl -k restart".

You might need to sudo if you are not a super user. Hope this helps.

EDIT: To check if your virtual host works. Run this sudo /usr/sbin/apachectl -k restart -S To see if code.local has been added as one of your virtual hosts.

Then try accessing code.local/index.php/welcome and code.local/welcome to check if it works.


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


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


Since your CI installation is not into your document root you should add RewriteBase /code/ to make it works.

This is my .htacces I use for a CI installation (this is little bit more elaborated than the one provided with the official documentation):

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /code/

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

Also make sure your httpd can handle .htaccess file. Check into httpd.conf if exists a line with AllowOverride all inside a <Directory></Directory> element

Hope this help


Try...

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


I have the same problem, the things is.

  1. htacces should be in the root of Codeigniter
  2. Remove "Deny from all" in your htacces file.
  3. Add the code on the post above


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.


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


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


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

Create an .htacess file on the root folder of your web directory in codeigniter using the above code. You will be able to remove index.php if an url link contains index.php so it will display page with no error. Or if url doesn't contains index.php it will display the page without index.php in url.


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?


From the words of the CodeIgniter documentation

By default, the index.php file will be included in your URLs:

example.com/index.php/news/article/my_article You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:

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

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

you can find it here: http://codeigniter.com/user_guide/general/urls.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号