简体   繁体   中英

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 .

At work, we use ASP Classic and have some file(s) that allows us to do things like:

<%
 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. 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).

I've seen files like ParseURL.asp and other files (at work) that somehow create these URLs and allow us to use them globally. I am trying to learn how this is done in PHP (At work, we run Windows Server, I have a Linux box).

What I am trying to do is create the following URLs for their respective pages:

And again, I would like to create these URLs WITHOUT creating directories for the paths.

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.

The http_build_url tutorials I've found show this:

<?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

Note: At work, we can also use the if path = "contact" to include certain snippets like this:

<%
 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!

Am I researching the right functions? Do I need to use some sort of URL Re-write function?

Try create .htaccess file in your index / document_root folder with this contents

  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.

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". 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. 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".

The .htaccess file needs to contain the following in order to achieve what I needed...

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.

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 . 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).

Now I highly recommend looking up "mod_rewrite" tutorials before dipping into this. 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 ).

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. I played around with the hrefs and got it dialed. Apparently these false "directories" impact hrefs as though they existed? Anyways, if you encounter this, don't panic :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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