Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • http://mysite.com/browse/ - browse.php
  • http://mysite.com/contact/ - contact.php
  • http://mysite.com/top100/ - top100.php

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://[email protected]/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?

like image 576
derekmx271 Avatar asked Dec 18 '25 03:12

derekmx271


2 Answers

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>
like image 63
mschr Avatar answered Dec 19 '25 18:12

mschr


If you have mod_rewrite installed, try the following .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]*) $1.php
like image 20
dpk2442 Avatar answered Dec 19 '25 20:12

dpk2442



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!