macos 从 URL 中删除“index.php” - Codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6809373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove "index.php" from URL - Codeigniter
提问by MacMac
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:
我查看了在index.php
访问不同视图时从 URL中删除的 Codeigniter 文档,代码显示了如何使用 apache 删除它:
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
However, when I go to http://localhost/code/home
, I get this:
但是,当我去时http://localhost/code/home
,我得到了这个:
The requested URL /code/home was not found on this server.
在此服务器上找不到请求的 URL /code/home。
But accessing http://localhost/code/index.php/home
works just fine, how come it isn't working for me?
但是访问http://localhost/code/index.php/home
工作正常,为什么它对我不起作用?
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.
我在 Mac OS X Snow Leopard 上使用Sites
目录: /Users/~myusername~/Sites/code
,我没有使用任何软件,即 MAMP、XAMPP。
EDIT
编辑
For the sudo /usr/sbin/apachectl -k restart -S
command, I get this:
对于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
采纳答案by ace
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.
您需要将“http://localhost/code”设置为“code.local”(在我的示例中)。假设您在 Mac OS X Snow Leopard 上的设置与我的相同。
You should add the following lines to "/etc/apache2/extra/httpd-vhosts.conf"
您应该将以下行添加到“/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"
还要确保在“/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
然后你将 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
Then restart your apache "/usr/sbin/apachectl -k restart".
然后重新启动你的apache“/usr/sbin/apachectl -k restart”。
You might need to sudo if you are not a super user. Hope this helps.
如果您不是超级用户,则可能需要 sudo。希望这可以帮助。
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.
编辑:检查您的虚拟主机是否工作。运行此 sudo /usr/sbin/apachectl -k restart -S 以查看 code.local 是否已添加为您的虚拟主机之一。
Then try accessing code.local/index.php/welcome and code.local/welcome to check if it works.
然后尝试访问 code.local/index.php/welcome 和 code.local/welcome 以检查它是否有效。
回答by Brad
Try this one
试试这个
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php? [L,QSA]
I tried 3 before I got one to work
在我得到一个工作之前,我尝试了 3 个
回答by Akhilraj N S
To remove index.php from URL just add
要从 URL 中删除 index.php 只需添加
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php//var/www/CodeIgniter/index.php
/var/www/CodeIgniter/.htaccess
[PT,L]
in the .htaccess file and move the .htaccess file near to index.php location
在 .htaccess 文件中并将 .htaccess 文件移动到 index.php 位置附近
$config['index_page'] = '';
in config.php in config folder set
在 config.php 中的 config 文件夹集中
<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?/ [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?/ [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?/ [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>
回答by Francesco Laurita
Since your CI installation is not into your document root you should add RewriteBase /code/
to make it works.
由于您的 CI 安装不在您的文档根目录中,您应该添加RewriteBase /code/
以使其正常工作。
This is my .htacces I use for a CI installation (this is little bit more elaborated than the one provided with the official documentation):
这是我用于 CI 安装的 .htacces(这比官方文档提供的要详细一点):
RewriteEngine on
RewriteBase /code/
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ code/index.php/ [L]
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
还要确保您的 httpd 可以处理 .htaccess 文件。如果元素AllowOverride all
内部存在一行,请检查 httpd.conf<Directory></Directory>
Hope this help
希望这有帮助
回答by toopay
Try...
尝试...
RewriteRule ^(.*)$ /index.php/ [L]
回答by Robin
I have the same problem, the things is.
我有同样的问题,事情是。
- htacces should be in the root of Codeigniter
- Remove "Deny from all" in your htacces file.
- Add the code on the post above
- htacces 应该在 Codeigniter 的根目录下
- 删除 htacces 文件中的“拒绝所有人”。
- 在上面的帖子中添加代码
回答by Box
You should change
你应该改变
RewriteRule ^(.*)$ /code/index.php/ [L]
to
到
$config['index_page'] = '';
You also need to modify the $config['index_page'] variable like this:
您还需要像这样修改 $config['index_page'] 变量:
<Directory "/Users/<your user name>/Sites/">
Options Indexes MultiViews FollowSymlinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
Please refer to my post at http://www.boxoft.net/2011/07/removing-index-php-from-codeigniter-2-0-2-url/if you like.
如果您愿意,请参阅我在http://www.boxoft.net/2011/07/removing-index-php-from-codeigniter-2-0-2-url/ 上的帖子。
回答by Vamsi Krishna B
in /etc/apache2/users/.conf
在 /etc/apache2/users/.conf
try adding ..
尝试添加..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
and restart Apache to check results ..
并重新启动Apache以检查结果..
回答by gilbert
copy the .htaccess on your main codeigniter folder, mine was localhost/codeigniter/ and then change the {RewriteBase / to RewriteBase /codeigniter/}
将 .htaccess 复制到您的主 codeigniter 文件夹中,我的是 localhost/codeigniter/ 然后将 {RewriteBase / 更改为 RewriteBase /codeigniter/}
回答by Mirza Qasim Ali
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.
使用上述代码在 codeigniter 中的 web 目录的根文件夹中创建一个 .htacess 文件。如果 url 链接包含 index.php,您将能够删除 index.php,因此它将显示没有错误的页面。或者,如果 url 不包含 index.php,它将显示 url 中没有 index.php 的页面。