[英]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.