为什么在加载首页时使用Index.php会出现404错误,但是在直接链接中使用index.php却可以正常工作?

23 浏览
0 Comments

为什么在加载首页时使用Index.php会出现404错误,但是在直接链接中使用index.php却可以正常工作?

我似乎无法理解为什么我的Homepage突然拒绝直接加载,即www.mysite.com/,但只有在键入www.mysite.com/index.php时才能加载。\n这是网站:\n对比:\n

\nhttp://www.propasiarealty.com // 将您发送到404页面\n

\n:\n

\nhttp://www.propasiarealty.com/index.php // 加载首页。\n

\n无法弄清楚到底出了什么问题,请帮忙。\n以下是Filezilla的截图:\n\"enter\n编辑01\n请查看我的HTACCESS...快照\n\"enter\n


\n更新02\n我已经修改了HTACCESS,但仍然没有成功,现在看起来像这样:\n

DirectoryIndex index.php
#ErrorDocument 400     /400.html
#ErrorDocument 401     /401.html 
ErrorDocument 403     /errs/404.php
ErrorDocument 404     /errs/404.php
#ErrorDocument 500     /500.html
Options -Indexes

\n但仍然没有起作用\n


\n更新03 [这个有效]\n根据@HPierce的建议,解决方案是将HTACCESS修改为以下内容:\n

DirectoryIndex index.php
RewriteEngine on  
RewriteRule ^$ index.php
    #ErrorDocument 400     /400.html
    #ErrorDocument 401     /401.html 
    ErrorDocument 403     /errs/404.php
    ErrorDocument 404     /errs/404.php
    #ErrorDocument 500     /500.html
    Options -Indexes

0
0 Comments

我在加载首页index.php时遇到了404错误,但是直接链接index.php却可以正常工作,为什么?

出现这个问题的原因是Apache的默认DirectoryIndex设置为index.html,而不是你想要加载的index.php页面。解决方法之一是在.htaccess文件中添加一个新的DirectoryIndex声明,如下所示:

DirectoryIndex index.php

另一种解决方法是同时指定两个文件作为DirectoryIndex,如下所示:

DirectoryIndex index.html index.php

这会首先检查index.html,如果index.html不可用,则加载index.php。这可能是更好的解决方案,因为更改默认设置可能会让其他人在你的网站上工作时感到困惑。

如果这些方法都不起作用,你可以考虑添加一个重写规则,尽管这并不推荐。重写规则会中断传入的HTTP请求并重新格式化,以匹配服务器的设置方式。如下所示:

RewriteEngine on

RewriteRule ^$ index.php

这个重写规则只匹配对`http://www.propasiarealty.com`的请求,不会匹配`http://www.propasiarealty.com/`。

使用重写规则需要安装mod_rewrite模块,更多信息请参考这里:[http://httpd.apache.org/docs/current/mod/mod_rewrite.html](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)

如果你有访问httpd.conf文件(Apache的全局配置文件)的权限,你可以分享一些文件内容,特别强调任何带有AllowOverrides的行。

你可以使用`$ apachectl -V`命令找到httpd.conf文件的位置(不同的环境位置可能不同),更多信息请参考这里:[stackoverflow.com/questions/12202021](stackoverflow.com/questions/12202021/…)

如果你不能访问httpd.conf文件,你可以尝试在.htaccess文件中添加以下内容(这是一个不推荐的脏hack):

RewriteEngine on
RewriteRule ^$ index.php

其中RewriteEngine和RewriteRule应该分别在不同的行上。你可以在这里进行测试,并证明它在适当配置的服务器上可以工作:[htaccess.madewithlove.be](http://htaccess.madewithlove.be/)

通过这个方法解决了问题后,你可以将以下内容添加到你的回答中,并将其作为解决方案接受:`RewriteEngine on RewriteRule ^$ index.php`。非常感谢你的建议!

0