PHP index.php 文件从子目录位置获取其他页面内容

PHP index.php File get other pages content from subdirectory location

如何在服务器根目录中只获取一个index.php,并使所有链接页面如/about.php /contact.php等位于/pages/dist等子目录中服务器?如果有人访问 example.com/about.php,它会从 /pages/dist/[filename.php] 获取内容?

我找到了两种方法。

  1. 将 htaccess 与正则表达式结合使用:

使用 rewrite engine,你 link 基础 url 到这样的文件夹:

RewriteEngine On
RewriteRule ^(?!index\.php)([\w-_]+)$ pages/dist/.php [QSA,L]
RewriteRule ^(?!index\.php)([\w-_]+\.php)$ pages/dist/ [QSA,L]

第二行是没有 .php 标签结尾的文件,第三行是。 如果用户在 url www.example.com/test.php 中输入,它将显示 www.example.com/page/dist/test.php,即使用户去掉了php标签,但是如果他输入了index.php它会保持不变。


  1. 在没有正则表达式的情况下使用 htaccess:

这个只是为了简单起见,但如果我们不使用正则表达式,它可能看起来像这样:

RewriteEngine ON
RewriteRule "test.php" "page/dist/test.php"
RewriteRule "test2.php" "page/dist/test2.php"
RewriteRule "test3.php" "page/dist/test3.php"
...

如有错误请指正