【问题标题】:Kohana: Completely remove index.php from url (not repeated issue)Kohana:从 url 中完全删除 index.php(不重复问题)
【发布时间】:2012-07-02 16:18:01
【问题描述】:

===== 澄清 =====

请注意这一点,与此类似的帖子有数百个答案,但人们只涉及问题的一部分,而不是我在这里问的真正问题。

=====

我正在使用 Kohana 3.2,我想从 url 中完全删除 index.php。比如我想访问控制器foo,action bar。

通常,正确设置引导程序和 htaccess,我可以这样访问:

www.domain.com/foo/bar.

但我也可以这样访问:

www.domain.com/index.php/foo/bar

我希望只允许用户像第一种方式那样访问,而不像第二种方式那样访问。

我在 bootstrap.php 中有这个部分:

Kohana::init(array(
    'base_url'   => '/',
    'index_file' => '',
));

我尝试取消 index_file 行,将其值更改为 FALSE,我仍然能够以两种方式访问​​ foo/bar。

我认为解决方案是修改 htaccess,但我尝试了一些更改,但它没有按我的意愿工作。我正在使用 kohana 附带的原始 htaccess:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /knisu/

# Protect hidden files from being viewed
<Files .*>
    Order Deny,Allow
    Deny From All
</Files>

# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|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]

任何帮助将不胜感激:)

编辑: 我在另一篇文章中找到了一个可以使它起作用的答案:

  # Redirect from:
  # http://localhost/index.php/welcome
  # to
  # http://localhost/welcome
  #
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^index\.php/?(.*)?$ $1 [R,L]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ ?/$1 [L]

问题是,作者说:“这可行,但问题在于存在的文件和目录,他必须放置图像、css、js 等的完整路径......我稍后会修复”

这可能有助于找到最终解决方案吗?

【问题讨论】:

    标签: kohana


    【解决方案1】:

    我认为使用原始的 htaccess 更好,但有改进。

    之后...

    RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
    

    这应该做“index.php/”的重定向,

    # Redirect any "index.php/user/profile/" to "/user/profile/"
    RewriteRule ^index.php/(.*)$ /$0 [L,R=302]
    

    根据我的经验,通过控制器传递 css/js 比纯 apache/nginx 请求慢。

    【讨论】:

    • 你能说清楚吗?我不知道我是否应该保留“RewriteCond %{REQUEST_FILENAME} !-f”和“RewriteCond %{REQUEST_FILENAME} !-d”。我尝试了两种方法,但没有像我在那里解释的那样工作。
    猜你喜欢
    • 2011-07-06
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多