【发布时间】:2014-07-19 01:49:46
【问题描述】:
如何删除在中心某处的 codeigniter 的每条路径中突出的"index.php"?
我想要干净的非index.php-fied URLs?
【问题讨论】:
标签: php mod-rewrite codeigniter url-rewriting
如何删除在中心某处的 codeigniter 的每条路径中突出的"index.php"?
我想要干净的非index.php-fied URLs?
【问题讨论】:
标签: php mod-rewrite codeigniter url-rewriting
第 1 步:
在 htaccess 文件中添加这个
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
第 2 步:
删除 codeigniter 配置中的 index.php
$config['base_url'] = '';
$config['index_page'] = '';
第 3 步:
允许在 Apache 配置(命令)中覆盖 htaccess
sudo nano /etc/apache2/apache2.conf 并编辑文件并更改为
AllowOverride All
对于 www 文件夹
第 4 步:
启用 apache mod rewrite(命令)
sudo a2enmod 重写
第 5 步:
重启 Apache(命令)
sudo /etc/init.d/apache2 restart
【讨论】:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Foldername of your ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
【讨论】:
解决codeigniter的url路径中的index.php有两种方法
1:在 config/config.php 更改代码:
$config['index_page'] = 'index.php';
到
$config['index_page'] = '';
2:在根路径下创建.htacess,如果没有创建,复制以下代码:-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
保存它,然后检查您的 Apache 配置 rewrite_module 或 mod_rewrite 是否已启用。如果没有,请启用。 (最佳方法)!
【讨论】:
在您的 .htaccess 中复制过去的以下代码
RewriteEngine on
Options -Indexes
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
【讨论】:
嗨,这个对我有用
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/index.php?/$1 [L]
还有这个
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/index.php?/$1 [L]
如果此 codeigniter 应用程序作为子域托管,则文件夹是子文件夹的名称 例如域名/文件夹/index.php。
希望对你有用。谢谢。
【讨论】:
这个给了我完整的魔法:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
希望对你有帮助!
【讨论】:
我在.htaccess 文件中使用这些行;我放在根文件夹中
RewriteEngine on
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
那我可以访问http://example.com/controller/function/parameter
而不是http://example.com/index.php/controller/function/parameter
【讨论】:
如果您在 linux 上并使用 apache2 服务器,那么除了 .htaccess 文件的更改之外,我们可能还需要覆盖 apache2.conf 文件。在 /etc/apache2/apache2.conf 上找到 apache2 配置文件。
搜索目录 /var/www/ 更改 AllowOverride None -> AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory
【讨论】:
第 1 步 => 将您的 .htaccess 文件放在应用程序和系统文件夹所在的根文件夹中。
第 2 步 => 如果您的网络路径使用子文件夹,例如 - yourdomain.com/project/ - 然后在 htaccess 文件中使用以下代码
RewriteEngine on
RewriteBase /project/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /project/index.php/$1 [L]
如果您的网络路径使用根文件夹(例如 yourdomain.com),则在 htaccess 文件中使用以下代码
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
【讨论】:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
【讨论】:
我尝试了很多解决方案,但我想出的是这样的:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots \.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
我会根据需要从 url 中删除 index.php。
【讨论】:
作为一个 .htaccess 文件,一个不错的选择是使用Kohana Framework提供的那个:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
这是一个经过深思熟虑的 .htaccess 文件。
【讨论】:
这会帮助你 将此代码粘贴到 .htacess 文件中的应用程序文件夹中
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
<IfModule mod_php5.c>
php_value short_open_tag 1
</IfModule>
【讨论】:
查看\application\config\config.php文件,有一个变量名为index_page
应该是这样的
$config['index_page'] = "index.php";
改成
$config['index_page'] = "";
然后如上所述,您还需要向.htaccess 文件添加重写规则,如下所示:
RewriteEngine on
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
它对我有用,希望你也一样。
【讨论】:
确保您已启用 mod_rewrite(我没有)。
要启用:
sudo a2enmod rewrite
另外,将AllowOverride None 替换为AllowOverride All
sudo gedit /etc/apache2/sites-available/default
终于……
sudo /etc/init.d/apache2 restart
我的 .htaccess 是
RewriteEngine on
RewriteCond $1 !^(index\.php|[assets/css/js/img]|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
【讨论】:
actions: stackoverflow.com/questions/14419757/…
我在 apache2 上对许多不同的主机进行了测试,效果很好。
使用这个htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
确保您有 enabled mod_rewirte 和 phpinfo();
然后在 config/config.php 中执行此操作:
$config['index_url'] = '';
|
| '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'] = 'AUTO';
如果它还不起作用,请尝试将 $config['uri_protocol']='AUTO' 更改为第 40/54 行的 application/config/config.php 文件中列出的之一:
有时我使用:REQUEST_URI 而不是 AUTO 或 "QUERY_STRING" 用于 goDaddy 托管
【讨论】:
上述所有方法对我来说都失败了,然后我发现我没有在 /etc/apache2 的虚拟主机文件中将 AllowOverride None 更改为 AllowOverride All /sites-available/default
<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>
...
【讨论】:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /codeigniter/index.php/$0 [PT,L]
使用项目文件夹名称“codeigniter”更改 RewriteRule .* index.php/$0 [PT,L] 后。它为我工作。谢谢大家的支持。
【讨论】:
查看system\application\config\config.php文件,有一个变量名为index_page
应该是这样的
$config['index_page'] = "index.php";
改成
$config['index_page'] = "";
然后如前所述,您还需要在.htaccess 文件中添加重写规则
注意:在 CodeIgniter v2 中,此文件已从系统文件夹移出到 application\config\config.php
【讨论】:
在应用程序根目录中拥有 .htaccess 文件以及 index.php 文件。 (检查 htaccess 扩展是否正确,Bz htaccess.txt 对我不起作用。)
并将以下规则添加到 .htaccess 文件中,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
然后在你的 application/config/config.php 文件中找到下面一行
$config['index_page'] = 'index.php';
将变量设置为空,如下所示。
$config['index_page'] = '';
就是这样,它对我有用。
如果它不起作用,请尝试将以下变量一一替换为这些参数('AUTO'、'PATH_INFO'、'QUERY_STRING'、'REQUEST_URI'和'ORIG_PATH_INFO')
$config['uri_protocol'] = 'AUTO';
【讨论】:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
这肯定行得通..试试看
【讨论】:
在您的网络根目录中放置一个 .htaccess 文件
您所做的任何调整 - 如果不满足上述要求 - 它将无法正常工作。通常它在系统文件夹中,它应该在根目录中。干杯!
【讨论】:
application 目录而不是根目录中。
我想我可能会添加
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
将是 .htaccess 并确保以下列方式编辑您的 application/config.php 变量:
替换
$config['uri_protocol'] = “AUTO”
与
$config['uri_protocol'] = “REQUEST_URI”
【讨论】:
我在删除 index.php 时遇到了一些大问题。作为一般规则,下面的 .htaccess 已经在多台服务器上进行了测试并且通常可以正常工作:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
如果您对此不满意,那么下一步就是调整您的配置文件。尝试其他一些 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';
如果您仍然没有任何运气,请尝试更改重写规则以包含您的子文件夹。如果您在开发服务器等上使用临时 URL,这通常是一个问题:
RewriteRule ^(.*)$ /subofolder1/subfolder2/index.php/$1 [L]
只需使用这些选项,应该可以工作。另外,请确保您的索引文件设置为:
$config['index_page'] = '';
祝你好运!
【讨论】:
如果您使用的是 Apache,请在您的 Web 根目录中放置一个 .htaccess 文件,其中包含以下内容:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
另一个不错的版本在这里:
【讨论】:
RewriteCond $1 !^\/(index\.php|assets|robots\.txt)
按照 CI wiki 中 this tutorial 中的说明使用 mod_rewrite。
【讨论】:
这是我的一个 CI 项目的 .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /projectFolder/index.php/$1 [L]
最后一行应该提供您需要的内容,但您可能需要也可能不需要“/projectFolder”,具体取决于您的文件夹结构的设置方式。祝你好运!
【讨论】: