【问题标题】:.htaccess alow few .htm and .html files only.htaccess 仅包含少数 .htm 和 .html 文件
【发布时间】:2011-11-13 07:12:59
【问题描述】:

我有一个 .htaccess 文件,其内容下方正在将我的 index.php?page=aboutus 更改为 aboutus.htm RewriteRule ^([^/.]+)(.html)/?$ index.php?page=$1

我正在使用 $_GET["page"] 使用 PHP 打印数据库中的内容,但如果有人键入 test.html 它正在检查数据库中的 test 数据,

如何使用 .htaccess 只允许 url 中的几个 .html 文件,例如 aboutus.html、home.html、projects.html 等...

谢谢

【问题讨论】:

  • 为什么不在您的 php 脚本中直接限制数据库访问?使用数组指定您允许用户查看的页面。
  • 谢谢你的回复,但我想要404 erroDocument

标签: apache .htaccess


【解决方案1】:
RewriteRule ^(aboutus|home|projects)\.html/?$ index.php?page=$1

附言。为什么要在您的网址中保留“.html”部分?他们并没有真正添加任何东西。

【讨论】:

    【解决方案2】:

    正如我在评论中所说,您可以直接在您的 php 脚本中进行验证。

    无论如何,如果您真的想使用 .htaccess 文件来做到这一点,您可以考虑使用 RewriteMap :

    Apache documentation.

    但是,如果您不熟悉它可能会有点复杂,如果验证适用于一小部分文件,最好使用RewriteCond 指令:

    RewriteCond %{REQUEST_URI} ^/(aboutus|home|projects)
    RewriteRule ^([^/\.]+)(.*)?$ index.php?page=$1 [L] 
    

    【讨论】:

    • @noorahmad 不客气,我编辑了代码,因为它存在一些问题(起始斜线和 uri 中第一段之后的可能路径)。我希望这会有所帮助。
    • 感谢您的回复,但我还是有一些问题。编辑:这行代码对我帮助很大:RewriteRule ^(aboutus|home|projects)\.html/?$ index.php?page=$1 以防万一有人遇到同样的问题
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2018-09-04
    • 2013-04-15
    相关资源
    最近更新 更多