【问题标题】:Why is .htaccess url rewrite not working codeigniter mamp?为什么.htaccess url rewrite 不起作用codeigniter mamp?
【发布时间】:2013-08-14 10:19:43
【问题描述】:

[编辑]:How to remove "index.php" in codeigniter's path 的副本
[那里的解决方案不适合我]

我是第一次尝试 codeigniter,而且我对 php 还是很陌生。 我想从我的网址中删除 index.php。

安装的代码点火器
用我自己的替换 index.php

我的 .htaccess 中有这个

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

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


#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

我的重写是肯定的,我在我的根目录中加载了一个 phpinfo 页面,它显示了加载的模块。 我还检查了 httpd.conf

当我从 NetBeans 运行时,我被定向到 localhost:8888/domain/index.php 并加载我的索引页面
当我转到 localhost:8888/domain 时,我的 index.php 也会加载

当我转到 localhost:8888/domain/welcome 或 localhost:8888/domain/welcome.php 时,我得到 404
The requested URL /index.php/welcome was not found on this server.
当我转到 localhost:8888/domain/index.php/welcome 时,我被定向到欢迎控制器,但它只加载了我的 index.php,但没有标记。

我也在 .htaccess 中试过这个:

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

我得到:

这也不起作用(我知道,他们都打算做几乎相同的事情):

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /
    # Hide the application and system directories by redirecting the request to index.php
    RewriteRule ^(application|system|\.svn) index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]

</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

我在 config/config.php 中

 $config['base_url']    = 'localhost:8888/domain.com/';
    $config['index_page'] = '';

【问题讨论】:

  • 我猜http:8888/domain/ 必须是http://domain:8888/base_url 但我不确定。
  • 抱歉,这是一个 typeo,但它没有解决问题:现在是:$config['base_url'] = 'localhost:8888/domain.com/';
  • 其实我刚刚注意到,去 domain/welcome 会给出错误信息:在此服务器上找不到请求的 URL /index.php/welcome。
  • 为什么用自己的替换 index.php?您不必删除 index.php,您只需编辑默认控制器名称和路由路径。
  • 是的,我也注意到了。但是这些 .htaccess 黑客并没有将 domain/index.php/welcome 或 domain/index.php/welcome.php 发送到 domain/welcome 这应该是正确的吗?哦好的。所以他们只会使用我想的路线吗?

标签: php .htaccess codeigniter mod-rewrite


【解决方案1】:

我用默认的 CI 索引替换了我自己的索引(也就是说,我将它恢复为开箱即用的状态)。
然后我将 domain/index.php/pages/view/page.php 路由到 domain/index.php/page.php
然后我运行了 Nishant 的 .htaccess 代码,虽然它似乎没有工作(静态的非样式版本的网站显示在通常的 url 但不是在没有 index.php 的 url 上)它可能帮助我找到了一个解决方案。
我也将RewriteEngine On 更改为RewriteEngine on,尽管我很确定我之前曾尝试过。 在这些步骤之后,我又回到了 sbaaaang 建议的代码(这有点像是以前尝试过的版本 id),这次它起作用了。

对于其他可能遇到 .htaccess 麻烦的人,我建议在它们开箱即用时留下它们并通过 tutes(而不是像我那样跳进去)。

我的 .htaccess 就这样结束了

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  


<Files "index.php">
AcceptPathInfo On
</Files>  
</IfModule>

<IfModule !mod_rewrite.c>

ErrorDocument 404 /index.php
</IfModule>

和配置变量如:

$config['base_url'] = '';
$config['index_page'] = '';

【讨论】:

  • 嗨@jsky 你的配置变量留空了吗?
  • 显然是的(不久前)
  • 节省了我的时间。谢谢
【解决方案2】:
 $config['base_url']    = 'http://'.$_SERVER['HTTP_HOST'].'/';
 $config['index_page'] = '';

然后.htaccess:

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

我还注意到你说“用我自己的替换 index.php” 这是错误的,将原始 Codeigniter index.php 文件留在 主根,否则你永远不会运行 codeigniter :D

【讨论】:

  • 谢谢。我在您的回答之前尝试了此代码但没有成功(我在问题中引用它)。我也尝试过使用不同的 base_url 和 index_page 配置。然而,几天后返回它,运行 Nishant 的代码后,这个代码现在对我有用。由于您的代码版本/在我的问题中已经存在,而 Nishant 的回答让我得到了答案,我觉得最好的办法是给你们两个投票并接受我自己的回答(详细说明我做了什么)(为此我无论如何都没有代表)。
【解决方案3】:

希望对你有帮助

您面临的问题是因为您删除了 index.php 并将其替换为您自己的 index.php。

而是将 codeigniter 文件夹放在你的本地主机上,并将 index.php 放在你的根目录下 [本地主机,在浏览器中打开以下网址。

http://localhost:8888/domain/index.php/welcome

如果它工作正常,意味着显示你的codeigniter的欢迎页面,那么你可以考虑从你的url中删除index.php,通过添加

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

在你的.htaccess文件中,重写条件的第二行将确保重写规则不会执行,当url中有index.php或static时,你可以将所有的静态内容放在静态文件夹。

现在当 url 中没有 index.php 或 static 时,rewrite rule 会重写它。

假设网址是

http://localhost:8888/domain/welcome

将被改写为

http://localhost:8888/domain/index.php/welcome

在任何情况下,您的代码都将通过仅包含 index.php 的 url 提供。

【讨论】:

  • 感谢您的回答 Nishant,它以某种方式引导我进行了有效的 .htaccess 索引替换。现在有效的代码是此处另一个代码片段的组合,而您的完整代码对我不起作用,所以我认为最好的办法是对这两个代码进行投票,并详细说明我为使其工作而采取的步骤并接受作为答案(我没有得到代表)。谢谢你的帮助。
【解决方案4】:

试试这个非常简单的代码,它对我来说很好用:

输入您的项目文件夹的名称而不是 project2 更改您的配置 base_url=" "

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

【讨论】:

  • 这个对我也不起作用,如果有兴趣比较,请参阅上面的那个。
猜你喜欢
  • 2011-04-04
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多