Solved

Codiing Non-Existent Links

Posted on 2016-11-27
4
24 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 108

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 51

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 108

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Kali Linux store / persist wireless password 3 22
resizeing PHP image 2 22
Not needed 13 55
Linux as a middle box 7 23
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now