如何编写 .htaccess 文件以使 CodeIgniters URL 路由工作? [英] How do I write a .htaccess file to make CodeIgniters URL routing work?

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

问题描述

我正在使用 CodeIgniter 运行 LAMP 环境.我希望能够使用它的 URL 模式,例如 http://localhost/controller/function/ID,但默认情况下它必须是 http://localhost/index.php/控制器/函数/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?

如果我的服务器上运行多个站点怎么办.我是否必须将它放在 /var/www/ 中并根据加载的目录指定条件?或者我可以把它放在 /var/www/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.

如果请求的 URL 实际不存在,我可以使它只映射到 MVC URL 吗?例如,http://localhost/articles/2009/some-article-name 不会是服务器上的目录,所以它会映射到 index.php并提供给 codeigniter,但 http://localhost/forum 存在于服务器上,但不应由 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 文件中:

and added this to a .htaccess file:

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

并将其保存在我的网站根目录中(在本例中为 /var/www/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,它会打印Hello there",但是 http://localhost/cms/hello 给出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 None挫败我的伟大计划.

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.

推荐答案

在你的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 文件夹所在的同一目录中,因此在您的 CodeIgniter 根目录中,您应该有

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

  • 系统/
  • 用户指南/
  • index.php
  • .htaccess
  • 许可证.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 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 访问控制器.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆