【问题标题】:Technique to create user friendly URLs [duplicate]创建用户友好 URL 的技术 [重复]
【发布时间】:2011-11-03 14:31:14
【问题描述】:

可能重复:
How to create friendly URL in php?

我在 Google 上阅读了很多关于转向的内容

profile.php?id=24 变成对用户更友好的东西。

我将如何重定向一个链接,如

www.site.com/profile/username

profile.php?id=userid (find from username)

谢谢

【问题讨论】:

  • How to create friendly URL in php?How can I make the URL search engine friendly?tons of others 的可能重复项。请在提问前使用搜索。
  • 其实初始链接一般更像www.site.com/profile/userid/username。用户名只是为了方便而在 URL 翻译中使用。例如,看看 SO 是如何工作的。
  • 你可以做一些研究。这个问题的答案已经在 Google 上找到了 530 万次
  • 我始终相信本网站用户的答案,而不是在 Google 上找到的随机文章。
  • @Cicada,我理解你所说的用户 ID 和用户名在一起的意思。比运行查询从用户名中查找 id 更有意义。

标签: php url-rewriting


【解决方案1】:

当您的用户注册时,使用 PHP 或 CGI 脚本创建一个名为“Username.txt”的文件并将其存储在名为 Profiles 的文件夹中(如您所拥有的那样)。并在文本文件中,使其生成数字(计数或散列?)或使用 Google 或 Apache 中的重写服务。

【讨论】:

    【解决方案2】:

    这是通过在 .htaccess 文件中创建 RewriteRule 或在 httpd.conf 中定义规则来完成的。 这适用于 Apache。我不确定如何在其他服务器上执行此操作。

    【讨论】:

      【解决方案3】:

      这可以通过 Apache mod_rewrite RewriteEngine 来实现。 .htaccess 示例:

      RewriteEngine On
      RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
      

      【讨论】:

        【解决方案4】:

        您可以通过 apache rewrite rules 做到这一点。

        Apache 将在内部将 /profile/username 之类的 URL 重写为 profile.php?username=username

        示例:(将其放在与 profile.php 相同目录的 .htaccess 中)

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

        如果您的 profile.php 脚本不接受 username 参数,您还可以在用户友好的 url 中包含用户 ID:

        RewriteEngine On
        RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2
        

        这会将/profile/username-id 之类的网址重写为profile.php?id=id

        见 apahe mod_rewrite documentation

        【讨论】:

          猜你喜欢
          • 2013-06-19
          • 2015-06-02
          • 1970-01-01
          • 1970-01-01
          • 2011-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 2010-12-27
          相关资源
          最近更新 更多