【问题标题】:mod_rewrite for folder and its containing pages文件夹及其包含页面的 mod_rewrite
【发布时间】:2015-06-28 20:38:41
【问题描述】:

.htaccess 代码:

RewriteEngine on
RewriteRule ^articles/([-a-z]+)\.html$ articles/article.php?slug=$1 [L]

我有一个文件夹 [articles],其中包含两个页面:

index.php 和 文章.php

第一页显示文章列表,第二页实际使用参数 [slug] 从数据库中获取实际文章并显示。

site.com/articles/this-is-an-article-title.html
site.com/articles/article.php?slug=this-is-an-article-title

代码运行正常,但问题是我在 index.php 上看不到文章列表,并且错误地通过了 RewriteRule。

我该如何阻止这种情况?

【问题讨论】:

  • 所以你是说/articles/index.php 是通过articles.php 传输的,即使它没有被RewriteRule 覆盖? - (顺便说一句,通常建议具有与虚拟传入请求路径相同的文件和文件夹。严格不需要它们匹配)。
  • 我要获取 /articles/ 和 /articles/this-is-an-article-title.html (目前我可以拥有第二个。/articles/ not loading /articles/index.php文件

标签: php apache mod-rewrite


【解决方案1】:

在请求 articles/ 目录时,您的 RewriteRule 可能会选择隐含的 /articles/index.html

  1. 按以下方式禁用:

     DirectoryIndex index.php
    
  2. 或者编写一个自定义的 RewriteRule 来防止任何自动映射:

     RewriteRule ^articles/*$ articles/index.php [L]
    
  3. 也可选择禁用Options -MultiViews

    (在您的情况下,这可能不是问题。但如果没有 RewriteLog / access.log 摘录很难说。)

【讨论】:

  • 我也可以有这种模式吗? site.com/articles/site.com/articles/2/site.com/articles/3/site.com/articles/4/site.com/articles/5/
猜你喜欢
  • 2011-12-01
  • 2014-09-19
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 2012-06-13
  • 2020-08-26
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多