Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Codiing Non-Existent Links

Posted on 2016-11-27
4
Medium Priority
?
164 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 111

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 60

Accepted Solution

by:
Julian Hansen earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
Suggested Courses

886 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