简体   繁体   English

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

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

I am trying to create URLs in PHP and use the path ( $path ) in if statements . 我试图在PHP中创建URL并在if语句中使用路径( $ path )。

At work, we use ASP Classic and have some file(s) that allows us to do things like: 在工作中,我们使用ASP Classic并有一些文件允许我们做以下事情:

<%
 if path = "checkout" then

 <!--#include file="ViewCheckout.asp"-->

 elseif path = "billing" then

 <!--#include file="ViewBilling.asp"-->

 end if
%>

We can use the ' if path = "checkout" ' pretty much anywhere on our site. 我们几乎可以在我们网站的任何地方使用' if path =“checkout” '。 The curious thing is that ' http://myworksite.com/checkout/ ' is the URL of the checkout page, yet there is no directory on our server called " checkout "... The above is just a couple of the list of ' if path = "blah" ' includes that appear in our 'start.asp' (The file that contains the base template of the pages). 奇怪的是' http://myworksite.com/checkout/ '是结帐页面的URL,但我们的服务器上没有名为“ checkout ”的目录...以上只是列表中的几个' if path =“blah” '包括出现在'start.asp'中的文件(包含页面基本模板的文件)。

I've seen files like ParseURL.asp and other files (at work) that somehow create these URLs and allow us to use them globally. 我已经看到像ParseURL.asp这样的文件和其他文件(在工作中)以某种方式创建这些URL并允许我们在全局使用它们。 I am trying to learn how this is done in PHP (At work, we run Windows Server, I have a Linux box). 我试图了解如何在PHP中完成(在工作中,我们运行Windows Server,我有一个Linux盒子)。

What I am trying to do is create the following URLs for their respective pages: 我要做的是为各自的页面创建以下URL:

And again, I would like to create these URLs WITHOUT creating directories for the paths. 而且,我想创建这些URL 而不创建路径的目录。

I have read up on *http_build_url* and *parse_url* , but I am having a hard time determining the way to create URLs in the manner mentioned above. 我已阅读* http_build_url ** parse_url * ,但我很难确定以上述方式创建URL的方法。

The http_build_url tutorials I've found show this: 我发现的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
);
?>

The thing is, I can't figure out if this just outputs ftp://ftp.example.com/pub/files/current/?a=b&a=c as text or if it is a way to create a valid URL... (This is obviously an FTP address...) - PHP.net 问题是,我无法弄清楚这是否只是输出ftp://ftp.example.com/pub/files/current/?a=b&a=c作为文本,或者它是否是一种创建有效URL的方法。 ..(这显然是一个FTP地址...) - PHP.net

Note: At work, we can also use the if path = "contact" to include certain snippets like this: 注意:在工作中,我们也可以使用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
%>

If anyone can give me some pointers on how to re-create this using PHP, I would be very grateful! 如果有人能给我一些关于如何使用PHP重新创建它的指示,我将非常感激!

Am I researching the right functions? 我在研究正确的功能吗? Do I need to use some sort of URL Re-write function? 我是否需要使用某种URL重写功能?

Try create .htaccess file in your index / document_root folder with this contents 尝试使用此内容在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]

For compatibility setups where overriding rewrite module is not allowed or not available, then wrap the codeblock with: 对于不允许或不可用覆盖重写模块的兼容性设置,请将代码块包装为:

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

If you have mod_rewrite installed, try the following .htaccess file. 如果安装了mod_rewrite,请尝试以下.htaccess文件。

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

After much research, I figured out how to do this the way I needed to... First, I inserted some code that made the index.php file "dynamic". 经过大量的研究,我想出了如何以我需要的方式做到这一点......首先,我插入了一些使index.php文件“动态”的代码。 I placed this where the "content" of each page was going to be displayed... 我把它放在每个页面的“内容”将要显示的地方......

<?
    $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';
?>

What this does is include the appropriate "content" file that I want to show depending on what page someone is trying to view. 这样做包括我想要显示的相应“内容”文件,具体取决于某人试图查看的页面。 I stored my different pages in the /pages/ directory of my site. 我将不同的页面存储在我网站的/pages/目录中。 You can change this to whatever you want to. 您可以将其更改为您想要的任何内容。

Next, (if you are running an APACHE server and have the mod_rewrite module running (most hosting services and APACHE boxes have this module active) we want to open (or create if none exists in your site's directory) the .htaccess file. This can be done with notepad or dreamweaver. Just name the file ".htaccess" and make sure you don't save it as a ".txt". 接下来,(如果您正在运行APACHE服务器并且运行mod_rewrite模块(大多数托管服务和APACHE框都激活了此模块),我们要打开(或创建在您的站点目录中不存在).htaccess文件。这可以用记事本或Dreamweaver完成。只需将文件命名为“.htaccess”,并确保不将其保存为“.txt”。

The .htaccess file needs to contain the following in order to achieve what I needed... .htaccess文件需要包含以下内容才能实现我所需要的...

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

The ^ marks the start of the string, the (.*) basically designates anything can be here, the / is the /about / (there is more to this), and the $ marks the end of the string I believe. ^标记字符串的开头, (.*)基本上指定可以在这里的任何东西, /是/ about / (还有更多), $标记字符串的结尾我相信。

Note the space after $ ... After the space, you state what the URL currently is (which is what the block of code at the beginning of my answer generated)... In my case it was index.php?page=about . 注意$ ...之后的空格。在空格之后,你说明当前的URL是什么(这是我生成的答案开头的代码块)...在我的情况下它是index.php?page = about The $1 is not a PHP variable, it represents the value of the first variable which is about (or whatever the page you are viewing is). $ 1不是一个PHP变量,它代表了第一个变量的值 大约是(或任何你正在浏览的页面)。

Now I highly recommend looking up "mod_rewrite" tutorials before dipping into this. 现在我强烈建议在深入研究之前查找“mod_rewrite”教程。 It is new to me and pretty confusing. 这对我来说是新的,非常令人困惑。 This video was helpful to me ( http://www.practicalecommerce.com/articles/394-Video-Tutorial-Eliminating-Dynamic-URLs-with-Mod-Rewrite ). 这段视频对我有帮助( http://www.practicalecommerce.com/articles/394-Video-Tutorial-Eliminating-Dynamic-URLs-with-Mod-Rewrite )。

Thanks everyone for the help here! 谢谢大家的帮助! As always, it's greatly appreciated! 一如既往,非常感谢! :) :)

Note Once I got this configured, my CSS was completely broken and none of my images showed up. 注意一旦我配置了这个,我的CSS就完全坏了,我的图像都没出现。 I played around with the hrefs and got it dialed. 我玩了hrefs并拨打了它。 Apparently these false "directories" impact hrefs as though they existed? 显然这些错误的“目录”会影响hrefs,就像它们存在一样? Anyways, if you encounter this, don't panic :) 无论如何,如果你遇到这个,不要惊慌:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM