本文介绍了我怎样写.htaccess文件,使codeIgniters URL路由的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running a LAMP environment with CodeIgniter. I want to be able to use its URL pattern, like, http://localhost/controller/function/ID, but by default it has to be http://localhost/index.php/controller/function/ID. The user guide says that I need a .htaccess file to make the former style possible, and uses this as an example:

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

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

But I don't understand how this works. Where should I put the .htaccess? Does it have to be in the server root?

如果我有多个网站在我的服务器上运行。我必须把它放在 /无功/网络/ 并指定基于什么目录加载条件?或者,我可以把它放在 /无功/网络/ site_using_ codeigniter / ?我不知道怎么甚至得到这个工作。

What if I have multiple sites running on my server. Do I have to put it in /var/www/ and specify conditions based on what directories are loaded? Or can I just put it in /var/www/site_using_codeigniter/? I'm not sure how to even get this to work.

我可以让这个只会映射到一个MVC的URL请求的URL实际上并不存在?例如,的http://本地主机/用品/ 2009 /部分,文章名不会是服务器上的目录,所以它会映射到的index.php 并给予codeigniter,但的http://本地主机/论坛的存在于服务器上,但不应该由codeigniter处理。

Can I make it so that it will only map to an MVC URL if the requested URL doesn't actually exist? For example, http://localhost/articles/2009/some-article-name wouldn't be a directory on the server, so it would map to index.php and be given to codeigniter, but http://localhost/forum would exist on the server but shouldn't be handled by codeigniter.

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

Any other things I should do with a .htaccess file? Anything good to read about them?

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

I changed my config file like this:

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

和加入这一个的.htaccess 文件:

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

和它保存在我的网站根目录下(在这种情况下, /无功/网络/ CMS /

and saved it in my sites root directory (in this case /var/www/cms/)

我走进/etc/apache2/conf.d/security(其中包括/etc/apache2/apache2.conf中),并这样做:

I went into /etc/apache2/conf.d/security (which is included by /etc/apache2/apache2.conf) and did this:

<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://本地主机/ CMS /你好给出了一个404错误。

It's still not working however. If I navigate to http://localhost/cms/index.php/hello, it prints "Hello there," but http://localhost/cms/hello gives a 404 error.

想法?

我已经通过我的Apache配置文件去挖掘,终于找到在哪里被定义的所有目录(我在服务器配置文件中从来没有搞砸解决此日期之前),发现讨厌的的AllowOverride无被挫败我的计划为伟大。

I had to dig through my apache configuration files to finally find where all the directories were defined (I had never messed around in server config files before this date) and found that pesky AllowOverride None that was foiling my plans for greatness.

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

Now it works perfectly. All of the answers here are viable and work.

推荐答案

在你的系统/应用/配置/ 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文件在同一目录中的系统文件夹的位置,以便在codeIgniter根目录下,你应该有

Add your .htaccess file to the same directory where the system folder is located, so in your CodeIgniter root directory, you should have

  • 系统/
  • user_guide /
  • 的index.php
  • 的.htaccess
  • LICENSE.TXT

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

Also, since you have multiple sites running on your server, you might want to have VirtualHosts. Add this to the last part your apache2.conf for each site that you have:

Listen *:11000
<VirtualHost *:11000>
     ServerAdmin [email protected]
     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访问网站://本地主机:11000 / 并使用访问控制器的http://本地主机:11000 /你好

You may now access the site from http://localhost:11000/ and access the controller using http://localhost:11000/hello.

这篇关于我怎样写.htaccess文件,使codeIgniters URL路由的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:20