Solved

Codiing Non-Existent Links

Posted on 2016-11-27
4
108 Views
Last Modified: 2016-11-28
Hi Experts

Can anyone tell me how  are non-existent links are coded, in general, and in PHP?

By Non-Existent Links, I mean how EE does it for example:

https://www.experts-exchange.com/questions/28983605/Determining-Project-Cost.html

I do not think that there really is a 28983605 directory, and a Determining-Project-Cost.html static file, since  these are created dynamically? In WordPress, they call it PermaLink.

Thank you
0
Comment
Question by:APD_Toronto
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41903384
My guess is that EE uses a similar concept to the WP PermaLink.  All of the HTTP requests are routed through a single, central script (maybe through /questions/) where the original URL is identified and the page that contains /28983605/ is rendered for the browser.  The part about /Determining-Project-Cost.html is SEO sugar, and is unnecessary to the request routing.  This is not usually a PHP activity (except in WordPress) but is instead facilitated by .htaccess and the HTTP routing scheme of the web server.
0
 

Author Comment

by:APD_Toronto
ID: 41903415
If I wanted to implement something like that, how would I?
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41903654
The process is called URL rewriting. This process involves redirecting ALL requests to a single entry point (script) and then using code in the target script routing the request internally to where it is supposed to go.

How you setup routing is dependent on your requirements.

A very simple example follows (assumes Apache, and that mod_rewrite is enabled)
1. Create an .htaccess file in root of web site
AddType x-mapp-php5 .php
Options +FollowSymLinks

RewriteEngine on
RewriteBase /
# REDIRECT REQUESTS FOR index.html TO OUR TEMPLATE
RewriteRule index\.html$ index.php [L,NC,QSA]

#IF THE REQUESTED FILE DOES NOT EXIST PASS THIS
#TO index.php WITH THE PAGENAME AS A PARAMETER
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)\.html$ index.php?page=$1 [L,NC,QSA]

Open in new window


2. Create index.php file to extract the relevant data from the URL
<?php
// THE page PARAMETER WILL CONTAIN THE URL SENT TO THE
// SCRIPT BY THE .htaccess
// IF IT DOES NOT EXIST WE DEFAULT TO THE home PAGE
$url = isset($_GET['page']) ? $_GET['page'] : 'home';

// EXPLODE THE PATH ON '/' TO GET THE DIFFERENT ROUTE
// PARAMETERS
$parts = explode('/',$url);

// ASSUME THE FIRST ELEMENT IN THE PATH IS THE NAME OF 
// THE PAGE (controller) WE WANT TO ACCESS
$page = array_shift($parts);

// CHECK TO SEE IF WE HAVE A PAGE THAT MATCHES
// DEFAULT TO home IF THE PAGE DOES NOT EXIST
if (!file_exists("/pages/{$page}.php")) {
   $page = 'home';
}
// INCLUDE THE PAGE FOR DISPLAY
require_once(""/pages/{$page}.php");

Open in new window


You can read more about this in this article https://www.experts-exchange.com/articles/13095/A-simple-PHP-master-Template-framework.html

You can also read more in this PAQ https://www.experts-exchange.com/questions/28982441/pretty-urls-for-products.html
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41904204
to implement something like that...
Learn about RESTful routing.  The HTTP Layer docs for Laravel / Lumen are particularly apt.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question