【问题标题】:Can't access website's root page无法访问网站的根页面
【发布时间】:2013-05-17 10:01:15
【问题描述】:

通常,访问我的网站时,qpcftw.cu.cc 会透明地加载页面 qpcftw.cu.cc/index.php,而不会在 url 中显示 /index.php 部分。我一直在尝试使用 .htaccess 摆脱 .php 扩展。这是我的 .htaccess 文件的内容:

<Files />
ForceType application/x-httpd-php
</Files>

RewriteEngine on
RewriteBase /

RewriteRule ^.htaccess$ - [F]

RewriteRule ^([A-z,0-9,_,-]+)?$              $1.php     [QSA]
RewriteRule ^([A-z,0-9,_,-]+)/index\.html$    $1.php     [QSA]

ErrorDocument 403 php/error.php
ErrorDocument 404 /404.php
ErrorDocument 405 php/error.php
ErrorDocument 408 php/error.php
ErrorDocument 500 php/error.php
ErrorDocument 502 php/error.php
ErrorDocument 504 php/error.php

我需要满足 3 个条件:

  1. qpcftw.cu.cc/index.php == qpcftw.cu.cc/index
  2. 访问qpcftw.cu.cc/forum/ 仍然会在qpcftw.cu.cc/forum/index.php 加载我的PHPBB 论坛
  3. qpcftw.cu.cc 透明地加载 index.php 文件,但 URL 中不显示它

到目前为止,我当前的 .htaccess 满足了前两个需求,但打破了第三个需求。帮助! :/

【问题讨论】:

    标签: php .htaccess web


    【解决方案1】:

    与其一味的加.php,不如先检查.php文件是否存在

    RewriteCond $1.php -f 
    RewriteRule ^([A-z0-9_-]+)?$              $1.php     [L,QSA]
    
    RewriteCond $1.php -f
    RewriteRule ^([A-z0-9_-]+)/index\.html$    $1.php     [L,QSA]
    

    这仍然适用于 1 和 2,并且 3 现在也适用,因为代码将测试 index.php.php 是否存在(请求是针对 index.php),但它不存在(我希望!),所以RewriteRule 将失败。

    我还从正则表达式类中删除了逗号(你不能用它们来表示多个组,把它放在那里只是意味着 URL 中允许使用逗号),并在规则中添加了 [L]ast 标志为了更好的性能

    【讨论】:

    • 很好,条件 3 现在满足了,但不幸的是 #2 坏了。也许这是我的链接中的错误。我的链接应该指向/file.php 还是仅指向/file
    • 对。第一条规则不适用于以 /forum 开头的 URL。将RewriteCond %{REQUEST_URI} !^/forum 添加到第一个 RewriteRule(RewriteCond $1.php -f 之后,在新的一行)
    • 看来/forum//forum 的工作方式是一样的
    • 糟糕,我的意思是它打破了 #1,而不是 2
    【解决方案2】:

    以下 .htaccess 代码自动完成所有扩展隐藏魔法:

    Options +MultiViews
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多