【问题标题】:How to create dynamic/friendly URLs using PHP?如何使用 PHP 创建动态/友好的 URL?
【发布时间】:2011-03-25 12:28:11
【问题描述】:

谁能解释如何创建友好的 URL?我的意思是像 http://store.steampowered.com/app/22600/ 这样的 URL,没有像 index.php 这样的任何页面可见。

【问题讨论】:

标签: php url dynamic friendly-url


【解决方案1】:

如果您只有 cpanel,请使用 .htaccess

如果这不起作用,您将在 php 中使用如下链接解析 url:

http://server.com/router.php/search

你可以用这样的东西来做到这一点。

<?
list($junk,$url) = explode("router.php",$_SERVER['REQUEST_URI']);
$paths = explode("/", $url);
if ($paths[0] == 'search')
{
   Header("Location: /search.php");
}
?>

【讨论】:

    【解决方案2】:

    您需要查找 apache mod_rewrite(假设您使用 apache 作为您的 Web 服务器)。 PHP 本身并不会为您完成大部分工作,Web 服务器会完成大部分工作。你需要告诉你的网络服务器使用 mod_rewrite 来指向所有匹配特定模式的 url 来指向你喜欢的任何 .php 文件。您可以选择任何方式在 url 模式中传递参数。例如使用 / 来分隔键值或只是值。如果您没有对服务器的 root 访问权限并且启用了它,您通常可以将这些 mod 重写规则放在站点根目录下的 .htaccess 文件中。

    Sitepoint 对初学者有很好的参考。 http://articles.sitepoint.com/article/guide-url-rewriting

    【讨论】:

      【解决方案3】:

      如果您使用的是 apache,这应该可以工作/是这个想法:

      RewriteRule ^(.*)$ /index.php?/$1 [L]
      

      现在您的网址将转到http://store.steampowered.com/index.php/app/22600/

      【讨论】:

        【解决方案4】:
        猜你喜欢
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        • 2011-02-24
        • 2015-06-02
        • 2015-11-03
        • 2022-01-02
        相关资源
        最近更新 更多