【问题标题】:How to create sub pages with php on user request?如何根据用户请求使用 php 创建子页面?
【发布时间】:2016-10-28 00:20:57
【问题描述】:

如果我的网站 url 类似于 someUrl.com 并且注册用户想向我的网站添加文章,我想用 php 创建一个子页面:

类似 someUrl.com/someSubPage

所以我的问题是:我应该使用 php 在服务器上创建一个新文件,还是应该将文章中的数据保存在数据库中并在每次用户请求时生成文件?如果我选择第二个选项,那么 url (someUrl.com/someSubPage) 呢?有可能还有这个网址吗?

提前谢谢你!:D

编辑: 好的数据库似乎是更好的方法。但是搜索引擎呢?我可以在没有框架的情况下做到这一点吗?

【问题讨论】:

  • 简而言之:数据库——绝对; url - 没问题,现在它们很少代表实际的目录结构;如何 - 更好地了解一个框架(如 Laravel)
  • 谢谢!:D 像谷歌这样的搜索引擎能找到这些页面吗?如果没有框架,这也可能吗?

标签: php url subdirectory


【解决方案1】:

是的,你可以在没有框架的情况下做到这一点。但是您可能会考虑使用 CodeIgniter,因为它非常易于安装和学习,而且重量不大。如果您不想这样做:您必须创建一个包含以下内容的 .htaccess 文件:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]


# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

如果在文件系统中找不到该文件,将加载页面 index.php。您可以使用 $_SERVER['REQUEST_URI'] 来获取请求的页面。

【讨论】:

  • 好的,谢谢!我真的应该了解一些关于 htaccess-files 的知识,因为我根本不明白这一点(除了 cmets)。但多亏了你,我现在知道这是可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多