如何在不使用PHP目录的情况下创建URL路径


How do you create URL paths without using directories with PHP?

我正在尝试在PHP中创建URL,并在if语句中使用路径($path)。

在工作中,我们使用ASP Classic,并有一些文件允许我们执行以下操作:

<%
 if path = "checkout" then
 <!--#include file="ViewCheckout.asp"-->
 elseif path = "billing" then
 <!--#include file="ViewBilling.asp"-->
 end if
%>

我们可以在网站上的任何地方使用'if path="checkout"'。奇怪的是"http://myworksite.com/checkout/'是结账页面的URL,但我们的服务器上没有名为"结帐"的目录。。。以上只是出现在"start.asp"(包含页面基本模板的文件)中的"if-path="blah""包含的几个列表。

我见过像ParseURL.asp这样的文件和其他文件(在工作中),它们以某种方式创建了这些URL,并允许我们在全球范围内使用它们。我正在尝试学习如何在PHP中实现这一点(在工作中,我们运行Windows Server,我有一个Linux盒子)。

我正在尝试为它们各自的页面创建以下URL:

  • http://mysite.com/browse/-浏览.php
  • http://mysite.com/contact/-联系人.php
  • http://mysite.com/top100/-top10.php

而且,我想再次创建这些URL而不为路径创建目录

我已经阅读了*http_build_url**parse_url*,但我很难确定以上述方式创建url的方法。

我找到的http_build_url教程显示了这一点:

<?php
echo http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
    array(
        "scheme" => "ftp",
        "host" => "ftp.example.com",
        "path" => "files/current/",
        "query" => "a=c"
    ),
    HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);
?>

问题是,我不知道这是否只是输出ftp://ftp.example.com/pub/files/current/?a=b&a=c作为文本,或者如果这是创建有效URL的一种方式。。。(这显然是一个FTP地址…)-PHP.net

注意:在工作中,我们还可以使用if-path="contact"来包含某些片段,如下所示:

<%
 if path = "contact" or path = "chat" or path = "checkout" then
 <div id="communication-nav">
  <ul>
   <li>Some link 1</li>
   <li>Some link 2</li>
   <li>Some link 3</li>
  </ul>
 </div>
 end if
%>

如果有人能给我一些关于如何使用PHP重新创建的建议,我将不胜感激!

我在研究正确的函数吗?我需要使用某种URL重写功能吗?

尝试在您的index/document_root文件夹中创建带有以下内容的.htaccess文件

  RewriteEngine on
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /
  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  # -- only if not a file
  RewriteCond %{REQUEST_FILENAME} !-f
  # -- only if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  # -- never for favicon
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  # -- rewrite if all above remains true
  # -- e.g. /perma-links/this-is-it  becomes index.php?q=perma-links/this-is-it
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

对于不允许或不可用重写模块的兼容性设置,则用包装代码块

<IfModule mod_rewrite.c>
     #rewrite rules
</IfModule>

如果安装了mod_rewrite,请尝试以下.htaccess文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]*) $1.php

经过大量研究,我找到了如何以我需要的方式做到这一点…首先,我插入了一些代码,使index.php文件"动态"。我把它放在了每个页面的"内容"将要显示的地方。。。

<?
    $p = $_GET['page'];
$page = "pages/".$p.".php";
if(file_exists($page))
    include($page);
elseif($p=="")
    include 'pages/home.php';
elseif($p=="about")
    include 'pages/about.php';
elseif($p=="contact")
    include 'pages/contact.php';
elseif($p=="disclaimer")
    include 'pages/disclaimer.php';
else
    include '404.html';
?>

这样做的目的是根据某人试图查看的页面,包括我想要显示的适当的"内容"文件。我将不同的页面存储在我的网站的/pages/目录中。你可以将其更改为你想要的任何内容。

接下来,(如果您正在运行APACHE服务器并运行mod_rewrite模块(大多数托管服务和APACHE框都激活了该模块),我们希望打开(或创建(如果您的站点目录中不存在).htaccess文件。这可以用记事本或dreamweaver完成。只需将文件命名为".htaccess",并确保不会将其保存为".txt".

.htaccess文件需要包含以下内容才能实现我所需要的。。。

RewriteEngine on
RewriteRule ^(.*)/$ index.php?page=$1

^标记字符串的开始,(.*)基本上指定任何可以在这里的东西,/是关于/的(还有更多),$标记字符串的结束。

注意$后面的空格。。。在空格之后,您说明URL当前是什么(这就是我答案开头的代码块生成的内容)。。。在我的情况下,它是index.php?page=关于$1不是PHP变量,它表示第一个变量的值,该变量是关于(或您正在查看的页面)的

现在,我强烈建议在深入研究之前先查阅"mod_rewrite"教程。这对我来说很新鲜,也很令人困惑。这个视频对我很有帮助(http://www.practicalecommerce.com/articles/394-Video-Tutorial-Eliminating-Dynamic-URLs-with-Mod-Rewrite)。

感谢大家的帮助!一如既往,非常感谢!:)

注意一旦我配置了这个,我的CSS就完全崩溃了,我的图像都没有显示。我和hrefs玩了一玩,然后拨通了电话。显然,这些虚假的"目录"会影响href,就好像它们存在一样?无论如何,如果你遇到这种情况,不要惊慌:)