【问题标题】:How i hide my page name in url with .htaccess?如何使用 .htaccess 在 url 中隐藏我的页面名称?
【发布时间】:2015-08-20 12:00:22
【问题描述】:

我的代码

RewriteRule ^walls/([0-9a-zA-Z-_]+) display.php?art_nm=$1 [NC,L]

我得到的是:

website.com/walls/i_love_coding

我需要的是:

website.com/i_love_coding

【问题讨论】:

  • 你可以使用 mod_rewrite
  • okae,你可以根据我上面的网址写代码

标签: php .htaccess url url-rewriting


【解决方案1】:

您可能应该使用front controller pattern。基本上,您将每个请求都发送给决定要做什么的前端控制器。

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule . /index.php [L]
</IfModule>

然后,使用您的“前端控制器”根据$_SERVER['REQUEST_URI'] 将请求转发到更正脚本。

index.php

<?php

$name = trim($_SERVER['REQUEST_URI'], '/');
$page = find_page_by_name($name);

if ($page) {
    require $page;
} else {
    require __DIR__.'/errors/404.php';
}

注意find_page_by_name 函数不要返回未经授权的文件的路径;你应该确保你的路径在一个特定的目录中(见realpathstrpos)。

例如,攻击者可以尝试获取http://example.com/../../etc/passwd !!!

对于更复杂的路由,您可能应该尝试像 SilexSlim 这样的微框架。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多