【问题标题】:htaccess rewrite with php pageshtaccess 用 php 页面重写
【发布时间】:2015-04-25 14:02:41
【问题描述】:

好的,首先让我说我明白这是一个以前被问过的问题。我只是无法将其缩小到关键字并找到我正在寻找的内容。如果这是重复的,请提前抱歉。 htaccess 重写对我来说就像黑魔法......我真的无法让它工作。

对于手头的问题: 我正在编写一个简单的准系统 php/html 站点。我已经有 10 年没有这样做了(我通常使用一些 CMS(wordpress、joomla 等))。我正在尝试处理这些 CMS 中“免费提供”的一些东西。喜欢漂亮的网址。 我有一个简单的 index.php,其中包含一些用于构建页面的内容。然后我有了包含动态内容的包含文件夹。

那么两个实际 URL 的例子

index.php?page=index(我的主页) index.php?page=anotherpage(另一页)

但是如果我想去 index.php?page=a-sub-page-to-another-page

这是我的 PHP(web 根文件夹中的 index.php)

if(isset($_GET["page"])){
        $page = $_GET["page"];
        $filename = "/includes/" . $page . ".php";
        if(file_exists($filename)){
            include("/includes/head.php");
            include("/includes/navbar.php");
            include $filename;
            include("/includes/footer.php");
        }else{
            include("/includes/404.php");
        }
    }else{
        include("/includes/head.php");
        include("/includes/navbar.php");
        include("/includes/index.php");
        include("/includes/footer.php");
    }

这是我的 .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1

只要我没有任何子页面,它就可以工作。但是如果我尝试去 [root]/somepage/somsubpage 那么它就不再起作用了。有人可以帮我吗?我希望复制我使用 wordpress 等标准 CMS 获得的效果,其中所有 URL 都是 SEO 友好的。

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1
    

    没有你的 php 变量 $_GET['page'] 将有完整的 url 例如:

    example.com/foo/barr

    所以 $_GET['page'] => /foo/barr

    但这是第一部分,只有您需要执行特殊功能才能将 url 映射到您的页面。

    做搜索引擎优化页面是存储做类似的事情:example.com/some-url/of-my-special/page-in-here.1111.html 所以这是你的网址,所以你需要看看在 .xxxxx.html xxxx 是可变的,所以现在如果你像这样写 htaccess:

    RewriteRule ^(.*)\.(\d+)\.html$ index.php?fullurl=$1&pageid=$2
    

    $_GET['fullurl'] => /some-url/of-my-special/page-in-here.1111.html $_GET['pageid'] => 1111

    【讨论】:

    • 感谢您的快速回复! :) 是的,我知道 PHP 是下一部分...但是如何?现在它只查找特定页面。如何根据子页面使其动态化?
    • 这真是个老话题:culttt.com/2011/11/16/… 做 SEO 页面的 iseast 方式是存储类似的操作:example.com/some-url/of-my-special/page-in-here .1111.html 所以这是你的网址,所以你需要查看 .xxxxx.html xxxx 是可变的,所以现在如果你像这样写 htaccess:RewriteRule ^(.*)\.(\d+)\.html$ index.php?fullurl=$1&pageid=$2
    • 好的,再次感谢您。但是,我有点不清楚这如何转化为动态命名结构?我必须为每个子页面执行此操作吗?必须有办法做到这一点,所以它只是读取 url 并翻译它?
    • 拜托!如果有人读到这个......我仍然坚持要做什么......除了mysql相关文章我找不到任何东西。我已经找了好几天了。我只需要了解如何在我的原始帖子中重写 PHP 以使用子页面,动态地使用变量。前任。 index.php?page=index, index.php?page=secondPage, index.php?page=secondPage&SubPage
    【解决方案2】:

    请!如果有人读到这个......我仍然坚持要做什么......而且我 除了 mysql 相关文章,找不到任何东西。我已经搜索过 天。我只需要了解如何在我的原始文件中重写 PHP 发布以使用子页面,动态使用变量。前任。 index.php?page=index, index.php?page=secondPage, index.php?page=secondPage&SubPage

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)$ index.php?level1=$1
    RewriteRule ^([^/]+)\/([^/])+$ index.php?level1=$1&level2=$2
    RewriteRule ^([^/]+)\/([^/])+\/([^/])+$ index.php?level1=$1&level2=$2&level3=$3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 2023-03-04
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多