从 URL 中删除 index.php - Codeigniter 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5155333/
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 2
提问by Paul
I am having trouble removing index.php from my URLs in Codeigniter. I've made a few websites with Codeigniter 1.7 and the .htaccess code I used doesn't work in 2.
我在 Codeigniter 中从我的 URL 中删除 index.php 时遇到问题。我用 Codeigniter 1.7 创建了一些网站,但我使用的 .htaccess 代码在 2 中不起作用。
I have tried using
我试过使用
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/ [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
</IfModule>
and
和
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/ [L]
</IfModule>
I've also tried it without RewriteBase / in.
我也试过没有 RewriteBase / in。
I have changed the $config['uri_protocol'] to REQUEST_URI and QUERY_STRING and nothing.
我已将 $config['uri_protocol'] 更改为 REQUEST_URI 和 QUERY_STRING ,什么也没有。
I have set $config['index_page'] = "";
我已经设置了 $config['index_page'] = "";
The file structure is 192.168.0.130/(site)/ so it must be going back to the root of the server and can't find the index.php file.
文件结构是192.168.0.130/(site)/,所以肯定是回到服务器根目录,找不到index.php文件。
All of the controllers I have made can be reached by putting 192.168.0.130/(site)/index.php/testcontroller
我制作的所有控制器都可以通过放置 192.168.0.130/(site)/index.php/testcontroller 来访问
Thank you.
谢谢你。
Apologies if this has been asked before - I have looked and tried what I could see.
抱歉,如果之前有人问过这个问题 - 我已经看过并尝试过我能看到的。
Edit:
编辑:
I should also add that I changed the default folders to be
我还应该补充一点,我将默认文件夹更改为
application
应用
CI-2.0
CI-2.0
index.php
索引.php
and changed the paths in index.php to be correct.
并将 index.php 中的路径更改为正确。
采纳答案by WNRosenberg
Try the first code block you posted, but instead of /index.php try using /(site)/index.php (obv replacing (site) with whatever your site folder is named).
尝试您发布的第一个代码块,而不是 /index.php 尝试使用 /(site)/index.php (obv 用您的站点文件夹命名的任何内容替换 (site))。
回答by Robin Morgan
This worked for me:
这对我有用:
- Create your htaccess file
- Set $config[‘index_page'] to an empty (config.php)
- string Set $config['uri_protocol'] = 'REQUEST_URI'; (config.php)
- 创建您的 htaccess 文件
- 将 $config['index_page'] 设置为空(config.php)
- string Set $config['uri_protocol'] = 'REQUEST_URI'; (config.php)
This is my htaccess file:
这是我的 htaccess 文件:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/ [L]
#When your application folder isn't in the system folder
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]
# 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
Source:
来源:
http://taggedzi.com/articles/display/codeigniter-2-htaccess-and-friendly-urls
http://taggedzi.com/articles/display/codeigniter-2-htaccess-and-friendly-urls
回答by saurabh kamble
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
This much is only required in root folder add this to .htaccess
这仅在根文件夹中需要,将其添加到 .htaccess
回答by Mário Carvalho
I was on this for 2 days.. Tried everything possible. I've just came out that you need the following:
我在这 2 天.. 尝试了一切可能。我刚刚发现您需要以下内容:
TYPE:
类型:
sudo nano /etc/apache2/sites-available/default
And change AllowOverride None to AllowOverride All - This will give access to your .htaccess files
并将 AllowOverride None 更改为 AllowOverride All - 这将允许访问您的 .htaccess 文件
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
And also enable mod rewrite on Apache:
并在 Apache 上启用 mod 重写:
http://stackoverflow.com/questions/3131236/how-do-you-enable-mod-rewrite
Hope this helps, Mário
希望这会有所帮助,马里奥
回答by neelabh
After following all instructions from other people and from this CodeIgnitor URL documnetation, don't forget to restart the server. I simply forgot to do that and kept trying all other solutions.
遵循其他人的所有说明和此CodeIgnitor URL 文档后,不要忘记重新启动服务器。我只是忘记这样做并继续尝试所有其他解决方案。
回答by daxter
You just need to add below code in .htacess.
您只需要在 .htacess 中添加以下代码。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
回答by tushar
1) Create .htaccess file in your root folder
1) 在你的根文件夹中创建 .htaccess 文件
RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L,QSA]
2) Edit application/config/config.php file in Application folder
2) 编辑 Application 文件夹中的 application/config/config.php 文件
$config['base_url'] = 'http://localhost/projectName';
$config['index_page'] = ''; // remove index.php
i hope it will work for you
我希望它对你有用