[英]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,但我们的服务器上没有名为“ checkout ”的目录...以上只是列表中的几个' if path =“blah” '包括出现在'start.asp'中的文件(包含页面基本模板的文件)。
我已经看到像ParseURL.asp这样的文件和其他文件(在工作中)以某种方式创建这些URL并允许我们在全局使用它们。 我试图了解如何在PHP中完成(在工作中,我们运行Windows Server,我有一个Linux盒子)。
我要做的是为各自的页面创建以下URL:
而且,我想创建这些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
^
标记字符串的开头, (.*)
基本上指定可以在这里的任何东西, /
是/ about /
(还有更多), $
标记字符串的结尾我相信。
注意$ ...之后的空格。在空格之后,你说明当前的URL是什么(这是我生成的答案开头的代码块)...在我的情况下它是index.php?page = about 。 该$ 1不是一个PHP变量,它代表了第一个变量的值 大约是(或任何你正在浏览的页面)。
现在我强烈建议在深入研究之前查找“mod_rewrite”教程。 这对我来说是新的,非常令人困惑。 这段视频对我有帮助( http://www.practicalecommerce.com/articles/394-Video-Tutorial-Eliminating-Dynamic-URLs-with-Mod-Rewrite )。
谢谢大家的帮助! 一如既往,非常感谢! :)
注意一旦我配置了这个,我的CSS就完全坏了,我的图像都没出现。 我玩了hrefs并拨打了它。 显然这些错误的“目录”会影响hrefs,就像它们存在一样? 无论如何,如果你遇到这个,不要惊慌:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.