只需要配置任何Web服务器,只要不指定任何内容,就必须查找名为index.php
的文件。自20世纪90年代初以来,这一直是所有Web服务器的一项非常标准的功能。在Apache中,您将使用DirectoryIndex指令;这就是我的样子:
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
然后,写在这样index.php
PHP代码充当路由器。您应该检查Variables From External Sources并了解$_GET
。
但是,这可能不是最好的布局。友好的URL已经存在多年:
http://example.com/contact
...并且它又是一个主要的web服务器功能。在Apache中,您可以使用mod_rewrite模块。下面是一些PHP框架使用的样本规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule^index.php [L]
请求的路径可以被解析出来的$_SERVER['REQUEST_URI']
。
一旦你把你的手在index.php
文件中有很大量的设计模式,你可以使用(现代的框架通常使用第三方router和template engine),但如果你是从头开始学习,只想从静态HTML中快速完成某项工作,您可以使用switch语句(创建路由白名单)和readfile()的组合来将每个文件注入输出。 (注意,PHP include构造族将处理文件作为PHP代码,这不是你想要的。)
<?php
define('INC_PATH', __DIR__ . '/../wherever/includes/are');
switch ($_GET['page']) {
case 'contact':
case 'help':
case 'whatever':
$page = $_GET['page'];
break;
default:
$page = 'error';
}
readfile(INC_PATH . '/index.html');
readfile(INC_PATH . '/page.html');
readfile(INC_PATH . '/footer.html');
readfile(INC_PATH . '/menu.html');
readfile(INC_PATH . "/$page.html");
尝试使用'。htaccess'文件重定向,因为你需要..见htaccess教程 – Sasikumar
但据我所知,.htaccess最终显示您的网页名称,而不是代码,因为我想,你有一个教程/或什么确切的东西我看在htaccess ...是301吗? – Kobxxxx
尝试在这里建议的方法https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/ – Sasikumar