Solved

Codiing Non-Existent Links

Posted on 2016-11-27
4
145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

630 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