【问题标题】:How do I restructure the htaccess to allow for the home page?如何重组 htaccess 以允许主页?
【发布时间】:2011-07-22 21:24:53
【问题描述】:

好的,所以我有一个包含大量页面的网站,我想为每个页面创建一个 php 文件...这很好用..这就是我所拥有的,代码明智

这是我的 htaccess

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

这是我的 index.php 文件

$parts = explode('/', $_REQUEST['url']);

switch ($parts[count($parts) - 1]) {
    case 'restaurant':
    include "pages/restaurant.php";
        break;
    case 'retail':
    include "pages/retail.php";
        break;  
        .......
        .......

}

这很好用,如果我访问 url http://someurl.com/restaurant,pages/restaurant.php 中的正确文件就会出现。唯一的问题是主页http://someurl.com 当我访问它时,我得到一个:

Forbidden

You don't have permission to access /structure on this server.

有没有办法在 .htaccess 中解决这个问题来解决这个问题,我应该在 pages 文件夹中创建一个名为 home.php 的文件,还是应该将主页的内容放在索引文件中其他条件...任何想法

【问题讨论】:

    标签: php file .htaccess url


    【解决方案1】:

    您忘记指定默认开关盒?

    switch ($parts[count($parts) - 1]) {
    case 'restaurant':
    include "pages/restaurant.php";
        break;
    case 'retail':
    include "pages/retail.php";
        break;  
    default:
    include "pages/default_page.php";
        break;
        .......
        .......
    }
    

    【讨论】:

    • 我什至无法访问该页面......如果.com 之后没有任何内容,浏览器将不会呈现任何内容
    • 你不明白我什至无法访问 php,因为 Forbidden You don't have permission to access /structure on this server.
    • @Tamer -- .com 后至少应该有斜线,例如http://www.example.com/
    【解决方案2】:

    1。在现有规则之前的某处使用此规则:

    RewriteRule ^$ index.php?url=home [L]
    

    2。创建pages/home.php

    3。将此添加到您的 switch 语句中:

    case 'home':
    include "pages/home.php";
        break;
    

    P.S. 我不建议使用default: 来处理主页。 default: 应该用于处理 404 页面/未知页面ONLY

    【讨论】:

    • 由于 seo 的原因我不能这样做...我需要 someurl.com 而不是 somesite.com?url=home
    • @Tamer -- 这与您已经使用的语法完全相同 -- 请参阅您自己的规则。为什么 SEO 对他们来说是好的,却变得坏了/不好??
    • 所以如果有人进入 somesite.com 我怎么得到它所以停止 Forbidden
    【解决方案3】:

    switch 中有 default 声明吗?我敢打赌你不会。

    【讨论】:

      【解决方案4】:
      DirectoryIndex index.php
      

      您的 httpd.conf 或 .htaccess 中有此代码吗?

      您的默认文件似乎不是 index.php

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 2018-03-24
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2013-08-10
        • 1970-01-01
        • 2017-07-03
        • 2013-10-08
        相关资源
        最近更新 更多