【问题标题】:.htaccess (removing index.php ) not working as explained in user guide.htaccess(删除 index.php )无法按照用户指南中的说明工作
【发布时间】:2013-12-14 06:30:07
【问题描述】:

我从现在开始一周前开始学习 codeigniter,我对它比较陌生,仍然通过 mvc 模式和 Codeigniter 找到我的方法

我面临的问题很奇怪,他们不确定我在做什么来导致这样的流程 我正在关注本教程http://ellislab.com/codeigniter/user-guide/general/urls.html 并且试图从我的站点中删除 index.php,即 localhost/Example.com/index.php

所以问题是我已将我的 .htaccess 文件放在 webroot 这是我的 Example.com 文件夹 我只是复制粘贴在下面用户指南中的代码并将其放在我的 .htaccess 中

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

我也从配置文件中删除了 index.php

所以现在每当我尝试访问 localhost/example.com 时,它都会让我回到 localhost 目录

我不知道是什么导致了这种流动

谁能帮助我谢谢我真的很想知道发生了什么以及我做错了什么

我正在使用 Wamp

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    有这样的规则:

    RewriteEngine on
    
    # remove index.php (external redirect)
    # if /index.php is part of original request to web server
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    # grab text before index.php in $1 and do an external redirect to $1
    RewriteRule ^(.*?)index\.php$ $1 [L,R=302,NC,NE]
    
    # internal rewrite for CI front controller
    # if request is not for a file
    RewriteCond %{REQUEST_FILENAME} !-f
    # if request is not for a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # if request is not for one of these listed files
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    # internally rewrite to index.php/<request-uri>
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    • THE_REQUEST 变量表示 Apache 从您的浏览器收到的原始请求

    参考:Apache mod_rewrite Introduction

    【讨论】:

    • 好的,现在试试编辑的代码。这个 .htaccess 是放在 DOCUMENT_ROOT 还是子文件夹中?
    • 哇哦。您编辑的代码有效,谢谢 man 。但它没有拿起我的样式表也放在 webroot baseurl ="localhost/Example.com"
    • 对于 css、js 等,只需在 css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。
    • 你是一个天才,感谢数百万你让它工作
    • 我希望你能向我解释这些规则,我不想成为一个愚蠢的程序员,只是在不知道背后逻辑的情况下使用东西,所以如果你有某种教程,或者如果你可以的话甚至在这里帮助我会很有帮助,我真的很想知道这些规则是什么意思......
    【解决方案2】:

    尝试在.htaccess 文件的顶部添加RewriteBase /example.com

    【讨论】:

    • 那行不通,它仍然带我回到 localhost 目录
    【解决方案3】:

    你在 Apache 中开启了 rewrite_module 吗?

    【讨论】:

    • 这一行在 httpd.confg LoadModule rewrite_module modules/mod_rewrite.so 中没有注释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多