【问题标题】:Facebook-like sub folders: How To?类似 Facebook 的子文件夹:如何?
【发布时间】:2011-10-15 17:32:43
【问题描述】:

我正在尝试创建一个与 facebook 类似的个人资料页面。我的问题是:我希望页面显示 www.sitename.com/Username,而不是显示 www.sitename.com/profile.php?username=Username 作为 URL。

我知道要显示一个像后一个一样的 URL,你会在一个名为 Username 的文件夹中有一个索引页面,但我不知道如何动态地执行它并将其显示为子文件夹。

我使用 php 作为我的主要语言。

【问题讨论】:

    标签: php subdirectory


    【解决方案1】:

    mod_rewrite for Apache 具有实现您想要的功能的功能。而且我确信其他主流网络服务器也有类似的能力。

    【讨论】:

      【解决方案2】:

      您可以使用 Apache 的 mod rewrite 实现友好的 url。只需在 Apache 配置中启用它并将以下内容放入 .htaccess,它将位于您的应用程序的根目录中:

      RewriteEngine On
      RewriteRule /(.*)$ /profile.php?username=$1 
      

      另外,here's 是一个关于友好 url 的漂亮教程。

      【讨论】:

      • 谢谢!这正是我想要的!
      【解决方案3】:

      您可以使用htaccesshttp.conf 或(如果您在 IIS 上)web.config

      我使用 htaccess。这是我如何允许动态配置文件的示例:

      RewriteEngine On 
      RewriteBase /
      RewriteRule ^profile\/(.*)$ ./profile.php?profileId=$1 [L,NC,QSA]
      

      RewriteBase / 表示./profile.php?profileId=$1 路径来自根目录。并且 RegExp ^profile\/(.*)$ 将匹配类似 profile/1234 的内容,从而显示 ./profile.php?profileId=1234

      希望对您有所帮助。

      【讨论】:

      • 您需要使用/ 而不是./ 否则Apache 将尝试加载profile/profile.php?... 作为文件,因为'profile' 当前充当目录。
      • RewriteBase 会处理这个问题。由于 htaccess 在 / 中,因此 ./ 如果来自 / 而不是 /profile/
      • 根据文档,RewriteBase 仅适用于相对路径(前面没有/./../)。一个./专门询问它应该使用当前目录来查找文件,即/profile/
      • ./ 是相对的。而且我一直都在使用它。
      • 我一直认为它是绝对的,因为有人告诉我 ./ 基本上替换为当前目录的完整路径,也就是 ./ => /path/to/directory/
      猜你喜欢
      • 2014-10-05
      • 2011-05-28
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多