如何将 /1.php 更改为 index.php?id=1


how to change /1.php to index.php?id=1

if (file_exists($filename)) {
echo "<a class='next' href='$filename'><img  src='images/next.png'</a>";
} 
else {
echo "<img src='images/next-dis.png'/>";
}

好吧,我正在尝试这样做,我将拥有像 1.php 2.php 3.php 这样的页面。但我希望它们看起来像这个"index.php?id=1"而不是/1.php、/2.php并希望阻止尝试访问的用户(例如(。2.php直接..谁能帮忙?..有什么简单的方法可以做到这一点吗?

Apache .htaccess文件中的以下重写规则应该可以解决问题。

RewriteEngine On
RewriteRule ('d+)'.php /index.php?id=$1

这基本上匹配[0-9]+.php格式中的任何 URL,并将其重定向到 ### 匹配 ###.php 的/index.php?id=###

搜索 .htaccess 重写规则。您将找到类似方案的示例。请参阅中的实用解决方案部分 - http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

在你的

索引中.php你可以这样做

include($_GET["id"]".php");

这将加载任何 id 是 get var,但是任何人都可以更改此变量并访问他们在此处输入的任何内容。