Solved

Codiing Non-Existent Links

Posted on 2016-11-27
4
85 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 109

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 54

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 109

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to count occurrences of each item in an array.

803 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