我怎样写.htaccess文件,使CodeIgniters URL路由的工作?(How do I w

2019-07-17 11:29发布

我正在使用CodeIgniter一个LAMP环境。 我希望能够利用它的URL模式一样, http://localhost/controller/function/ID ,但默认情况下它必须是http://localhost/index.php/controller/function/ID 。 用户指南说,我需要一个.htaccess文件,使前者的风格可能,并使用此作为一个例子:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

但我不明白这是如何工作。 我应该在哪里放置的.htaccess? 是否必须在服务器根目录?

如果我有我的服务器上运行多个站点。 我必须把它/var/www/并指定基于加载哪些目录的条件? 或可我只是把它/var/www/site_using_codeigniter/ ? 我不知道怎么甚至得到这个工作。

我可以让这个只会映射到一个MVC URL请求的URL实际上并不存在? 例如, http://localhost/articles/2009/some-article-name不会是服务器上的目录,所以它会映射到index.php ,并给予笨,但http://localhost/forum 存在于服务器上,但不应该由笨处理。

任何其他事情,我应该用.htaccess文件呢? 什么都好,了解他们吗?

更新

我改变了我的配置文件是这样的:

$config['index_page'] = ""; //used to be "index.php"

并加入这一个.htaccess文件:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

在我的网站根目录下保存它(在这种情况下, /var/www/cms/

我走进/etc/apache2/conf.d/security(由/etc/apache2/apache2.conf中含)这样做:

<Directory />
    AllowOverride All #was "none", but this was all commented out
    Order Deny,Allow
    Deny from all
</Directory>

它仍然没有工作。然而。 如果我浏览到http://localhost/cms/index.php/hello ,它打印“你好”,但http://localhost/cms/hello给出了一个404错误。

想法?

更新:绿色的成功!

我通过我的Apache配置文件去挖掘,终于发现,所有的目录被定义(我从来没有在服务器配置文件搞砸周围此日期之前),发现讨厌AllowOverride None这是挫败我的计划伟大。

现在,它完美的作品。 这里所有的答案都是可行和工作。

Answer 1:

在您的system/application/config/config.php ,变更

$config['index_page'] = "index.php";

$config['index_page'] = "";

并在您.htaccess ,补充一点:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

.htaccess文件添加到在同一个目录下system文件夹的位置,以便在你的笨根目录下,你应该有

  • 系统/
  • 用户指南/
  • 的index.php
  • 的.htaccess
  • LICENSE.TXT

此外,由于你有你的服务器上运行多个站点,你可能希望有VirtualHosts。 此加入最后一部分的apache2.conf为您的每个站点:

Listen *:11000
<VirtualHost *:11000>
     ServerAdmin you@somewhere.com
     DocumentRoot "/var/www/cms"
     ServerName you-dummy.com
     <Directory "/var/www/cms">
          AllowOverride All
          Options +Indexes
          DirectoryIndex index.php
          Order allow,deny
          Allow from all
     </Directory>
     ErrorLog logs/cms_log
     CustomLog logs/cms_log common
</VirtualHost>

您现在可以从访问该网站http://localhost:11000/和使用访问控制http://localhost:11000/hello



Answer 2:

这里尝试了答案,但没有为我工作。 所以我尝试以下,并没有...

创建在根目录下沿的index.php与LICENSE.TXT是可用的.htaccess。 在文件中输入以下代码,并将其存储:

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  

完成了! 尝试它,它肯定会工作。 希望能帮助到你



Answer 3:

有一些问题,找到从哪里变了“的AllowOverride全部”。

从本指南中 ,我发现了(默认)文件。

删除的index.php笨URL在Ubuntu

  1. 初步说明。 我运行的所有步骤在本教程中使用root权限,所以一定要确保你以root身份登录:

     sudo su 
  2. 启用Apache的mod_rewrite的模块。 首先启用该模块如下:

     a2enmod rewrite 

    有“AllowOverride无”的所有发生更改设置“AllowOverride全部”。 基本上所有在以下文件中“无”到“全部”:

      gedit **/etc/apache2/sites-available/default** 

    重新启动Apache:

     /etc/init.d/apache2 restart 
  3. 对笨打开文件config.php文件(应用程序\ config目录)。 上除去的index.php $config['index_page'] = "index.php";

     $config['index_page'] = ""; 
  4. 就笨根目录下文件的.htaccess。 填补这个代码的文件:

     RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] 

现在的index.php上笨URL消失



Answer 4:

这种特殊的.htaccess文件看起来像它需要在网站根目录去(这意味着,当前正在执行的网站。)

看看到文档RewriteCond了解什么其他种类的条件,您可以指定。 基本上是一个RewriteRule ,如果所有其前述的将只应用于RewriteCond s的匹配。 你可以尝试添加这些规则,如果请求的URL不存在只重定向:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

mod_rewrite的功能非常强大,但它可以是一个真正的痛苦,如果它不是做你认为它应该。 我不知道有什么方法来调试它的指令,无论是。

至于您可以使用.htaccess文件做些什么,在Apache的文档htaccess的文件总是以一个良好的开端。 最常见的用途是为每个目录的认证。 大多数服务器配置指令在.htaccess文件(提供服务器支持的的AllowOverride是这么说的),但有些不同的语法。



Answer 5:

把,在你的网站上的文档根目录。

如果您有多个站点,把它在每一个这样的网站的顶部。

如果您有来自同一个目录中运行多个站点,您可以使用的RewriteCond排除或包括特定的站点重写规则。

您可以排除某些目录(如/条)相同的方式。



Answer 6:

我有同样的问题与CodeIgnitor链接的图像,JS,CSS。 它所有的工作,现在感谢你们! 我按照下面的步骤。 综合所有其他意见到我的方案的工作。 希望这可以帮助别人。

  1. 改变的httpd.conf

     <Directory --your root dir-- /> AllowOverride All #was "none", but this was all commented out Order Deny,Allow Deny from all </Directory> 
  2. 创建在htdocs中\ yoursite(现场根)目录空.htaccess文件并粘贴以下。

     DirectoryIndex index.php RewriteEngine on RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 


Answer 7:

试试这个,它的工作原理perfetct

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  


Answer 8:

<IfModule rewrite_module>
RewriteEngine on
RewriteBase /

# Removes the "/" at the end
RewriteRule (.+)/$ /$1 [L,R]

RewriteCond $1 !^(index\.php|dhtml|img|css|js|favicon\.ico|robots\.txt)
RewriteRule ^([^?&]+) /index.php/$1 [L]
</IfModule>


Answer 9:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

简单的一个在.htaccess的根文件夹! 我的作品。



文章来源: How do I write a .htaccess file to make CodeIgniters URL routing work?