我查看了Codeigniter的文档,在访问不同的视图时从URL中删除了index.php
,代码显示了如何使用apache删除它:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
然而,当我去http://localhost/code/home
时,我明白了:
在此服务器上找不到请求的URL /代码/ home。
但访问http://localhost/code/index.php/home
工作得很好,为什么它对我不起作用?
我使用的是Sites
目录:/Users/~myusername~/Sites/code
我在Mac OS X Snow Leopard上,我没有使用任何软件,即MAMP,XAMPP。
修改
对于sudo /usr/sbin/apachectl -k restart -S
命令,我得到了这个:
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
答案 0 :(得分:8)
您需要将“http:// localhost / code”作为“code.local”(在我的示例中)创建您的Web根目录。 假设您在Mac OS X Snow Leopard上的设置与我的相同。
您应该将以下行添加到“/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>
还要确保在“/etc/apache2/httpd.conf”中取消注释
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
然后你将code.local添加到你的“/ etc / hosts”,它应该是这样的
##
# 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
然后重启你的apache“/ usr / sbin / apachectl -k restart”。
如果您不是超级用户,则可能需要sudo。 希望这会有所帮助。
编辑: 检查虚拟主机是否有效。运行这个 sudo / usr / sbin / apachectl -k restart -S 要查看code.local是否已添加为您的虚拟主机之一。
然后尝试访问code.local / index.php / welcome和code.local / welcome以检查它是否有效。
答案 1 :(得分:7)
试试这个
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]
在我找到工作之前我试过了3次
答案 2 :(得分:6)
要从URL中删除index.php,只需添加
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
<。>在.htaccess文件中并将.htaccess文件移到index.php位置附近
/var/www/CodeIgniter/index.php
/var/www/CodeIgniter/.htaccess
配置文件夹集中的config.php中的
$config['index_page'] = '';
答案 3 :(得分:3)
由于您的CI安装不在您的文档根目录中,因此您应添加RewriteBase /code/
以使其正常工作。
这是我用于CI安装的.htacces(这比官方文档提供的更精细):
<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>
还要确保您的httpd可以处理.htaccess文件。如果在AllowOverride all
元素
<Directory></Directory>
行,请检查httpd.conf
希望这个帮助
答案 4 :(得分:2)
...试
RewriteEngine on
RewriteBase /code/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ code/index.php/$1 [L]
答案 5 :(得分:2)
我有同样的问题,事情是。
答案 6 :(得分:1)
你应该改变
RewriteRule ^(.*)$ /index.php/$1 [L]
到
RewriteRule ^(.*)$ /code/index.php/$1 [L]
您还需要修改$ config ['index_page']变量,如下所示:
$config['index_page'] = '';
如果您愿意,请参阅http://www.boxoft.net/2011/07/removing-index-php-from-codeigniter-2-0-2-url/上的帖子。
答案 7 :(得分:0)
/etc/apache2/users/.conf
中的
尝试添加..
<Directory "/Users/<your user name>/Sites/">
Options Indexes MultiViews FollowSymlinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
并重启Apache以检查结果..
答案 8 :(得分:0)
复制主codeigniter文件夹上的.htaccess,我的是localhost / codeigniter /然后更改{RewriteBase / to RewriteBase / codeigniter /}
答案 9 :(得分:0)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
使用上面的代码在codeigniter中的web目录的根文件夹上创建一个.htacess文件。如果url链接包含index.php,您将能够删除index.php,因此它将显示没有错误的页面。或者如果url不包含index.php,它将在url中显示没有index.php的页面。
答案 10 :(得分:-1)
你所有的uri部分都被重写了,所以它指向/index.php/code/home。这样做的好方法是设置映射,例如。 http://code.local指向您的/ Users / ~myusername~ / Sites / code,然后您的所有请求都在根级别解析,一切都会正常工作。
编辑:Downvoters似乎是无知者,但没关系......以下是您的代码现在所做的事情:
GET http://localhost/code/home
// rewritten to:
http://localhost/index.php/code/home
就像我之前所说的GOOD方式将是主机映射所以你得到根级别的所有内容(这很好,因为你很可能会在生产服务器上以这种方式使用它......)这样就可以了随着.htaccess的变化。
否则你可以使用Francesco的RewriteBase建议,但有点改变:
// Francesco's RewriteRule does this:
http://localhost/code/home
// rewritten to"
http://localhost/code/index.php/code/home
所以只需将RewriteRule更改为:
RewriteRule ^code/(.*)$ index.php?/$1 [L]
// rewrites in your case to:
http://localhost/code/index.php/home
// which is what you wanted, aye?
答案 11 :(得分:-1)
来自CodeIgniter文档的话
默认情况下,index.php文件将包含在您的网址中:
example.com/index.php/news/article/my_article 您可以使用带有一些简单规则的.htaccess文件轻松删除此文件。下面是这样一个文件的示例,使用“否定”方法,除了指定的项目之外,所有内容都被重定向:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
在上面的示例中,除index.php,images和robots.txt之外的任何HTTP请求都被视为对index.php文件的请求。
你可以在这里找到它:http://codeigniter.com/user_guide/general/urls.html