如何删除codeigniter路径中的“index.php”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1445385/
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
How to remove "index.php" in codeigniter's path
提问by OrangeRind
How do I remove the "index.php"sticking out in every path in codeigniter somewhere in the center?
I want clean non index.php-fied URLs?
如何去除"index.php"中间某个地方的codeigniter中每条路径中的突出部分?我想要干净的非index.php-fied URLs?
采纳答案by Sean Vieira
If you are using Apache place a .htaccess file in your root web directory containing the following:
如果您使用 Apache,请将 .htaccess 文件放在您的根 Web 目录中,其中包含以下内容:
RewriteEngine on
RewriteCond !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
Another good version is located here:
另一个不错的版本位于此处:
回答by jimbo2087
I had some big issues with removing the index.php. As a general rule the .htaccess below has been tested on several servers and generally works:
我在删除 index.php 时遇到了一些大问题。作为一般规则,下面的 .htaccess 已经在多台服务器上进行了测试,并且通常可以正常工作:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
<Files "index.php">
AcceptPathInfo On
</Files>
If you don't have any luck with that then the next step is to adjust your config file. Try some of the other URI protocols e.g.
如果您对此没有任何运气,那么下一步是调整您的配置文件。尝试一些其他的 URI 协议,例如
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
$config['uri_protocol'] = 'ORIG_PATH_INFO';
If your still not having any luck try changing the rewrite rule to include your subfolder. This is often a problem if your using a temporary URL on a dev server etc:
如果您仍然没有任何运气,请尝试更改重写规则以包含您的子文件夹。如果您在开发服务器等上使用临时 URL,这通常是一个问题:
RewriteRule ^(.*)$ /subofolder1/subfolder2/index.php/ [L]
Just play around with these options, one should work. Also, make sure your index file is set to:
只是玩弄这些选项,一个应该工作。另外,请确保您的索引文件设置为:
$config['index_page'] = '';
Good luck!
祝你好运!
回答by Shaolin
Have the.htaccess file in the application root directory, along with the index.php file. (Check if the htaccess extension is correct , Bz htaccess.txt did not work for me.)
在应用程序根目录中有 .htaccess 文件,以及 index.php 文件。(检查 htaccess 扩展名是否正确,Bz htaccess.txt 对我不起作用。)
And Add the following rules to .htaccess file,
并将以下规则添加到 .htaccess 文件中,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
Then find the following line in your application/config/config.php file
然后在您的 application/config/config.php 文件中找到以下行
$config['index_page'] = 'index.php';
Set the variable empty as below.
将变量设置为空,如下所示。
$config['index_page'] = '';
That's it, it worked for me.
就是这样,它对我有用。
If it doesn't work further try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one
如果它不起作用,请尝试用这些参数('AUTO'、'PATH_INFO'、'QUERY_STRING'、'REQUEST_URI'和'ORIG_PATH_INFO')一一替换以下变量
$config['uri_protocol'] = 'AUTO';
回答by Re0sless
Have a look in the system\application\config\config.phpfile, there is a variable named index_page
查看system\application\config\config.php文件,有一个名为的变量index_page
It should look like this
它应该是这样的
$config['index_page'] = "index.php";
change it to
将其更改为
$config['index_page'] = "";
Then as mentioned you also need to add a rewrite rule to the .htaccessfile
然后如前所述,您还需要向.htaccess文件添加重写规则
Note: in CodeIgniter v2 this file was moved out of the system folder to application\config\config.php
注意:在 CodeIgniter v2 中,这个文件被移出系统文件夹 application\config\config.php
回答by waqas
All above methods failed for me and then I found that I was not changing AllowOverride Noneto AllowOverride Allin my virtual host file at /etc/apache2/sites-available/default
以上所有方法对我来说都失败了,然后我发现我没有在/etc/apache2/sites-available/default 的虚拟主机文件中将AllowOverride None更改为AllowOverride All
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All <---- replace None with All
</Directory>
<Directory /var/www >
Options Indexes FollowSymLinks MultiViews
AllowOverride All <--- replace None with All
Order allow,deny
allow from all
</Directory>
...
回答by Captain Sparrow
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /codeigniter/index.php/RewriteEngine on
RewriteBase /project/
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /project/index.php/ [L]
[PT,L]
after changing RewriteRule .* index.php/$0 [PT,L] with the project folder name "codeigniter".its working for me. Thanks guys for your support.
使用项目文件夹名称“codeigniter”更改 RewriteRule .* index.php/$0 [PT,L] 后。它对我有用。谢谢大家的支持。
回答by vinod
Step 1 => Place your .htaccess file in root folder where application and system folders exist.
Step 2 => If your web path using sub-folder like - yourdomain.com/project/ - then use following code in htaccess file
步骤 1 => 将您的 .htaccess 文件放在应用程序和系统文件夹所在的根文件夹中。
第 2 步 => 如果您的网络路径使用子文件夹,如 - yourdomain.com/project/ - 然后在 htaccess 文件中使用以下代码
RewriteEngine on
RewriteBase /
RewriteCond !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/ [L]
If your web path using root-folder like - yourdomain.com - then use following code in htaccess file
如果您的网络路径使用根文件夹,如 - yourdomain.com - 然后在 htaccess 文件中使用以下代码
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
[PT,L]
回答by Ramaraju.d
$config['uri_protocol'] = “AUTO”
This must definitely work.. have a try
这肯定有用..试试看
回答by Jefkine Kafunah
Just thought i might add
只是想我可以添加
$config['uri_protocol'] = “REQUEST_URI”
would be the .htaccess and be sure to edit your application/config.php variable in the following manner:
将是 .htaccess 并确保以下列方式编辑您的 application/config.php 变量:
replace
代替
sudo a2enmod rewrite
with
和
sudo gedit /etc/apache2/sites-available/default
回答by miguelitomp
Ensure you have enabled mod_rewrite(I hadn't).
To enable:
确保您已启用mod_rewrite(我没有)。
启用:
sudo /etc/init.d/apache2 restart
Also, replace AllowOverride Noneby AllowOverride All
另外,替换AllowOverride None为AllowOverride All
RewriteEngine on
RewriteCond !^(index\.php|[assets/css/js/img]|robots\.txt)
RewriteRule ^(.*)$ index.php/ [L]
Finaly...
最后...
##代码##My .htaccess is
我的 .htaccess 是
##代码##
