Solved

Base URI and Pretty Links

Posted on 2011-02-16
3
368 Views
Last Modified: 2012-06-27
I am working on some PHP scripts which are to work on various different web servers. At the moment the scripts work with urls like the following:

http://example.com/something/special?a=1&b=2&c=3

I need to make it possible to switch from the above format, to the below format for servers where the above is either not possible or more complicated to configure (for end-user):

http://example.com/index.php?route=something/special&a=1&b=2&c=3

All URIs utilise a global variable which presents the start of the entire address:
define('HTTP_BASE_URI', 'http://example.com/');

Open in new window

But obviously the following equivalent will also be required:
define('HTTP_BASE_URI', 'http://example.com/index.php?route=');

Open in new window


The base URI is used as follows to produce the first URI, but the second scenario renders an invalid URI because it contains two ? symbols:
echo HTTP_BASE_URI . 'something/special?a=1&b=2&c=3';

Open in new window


Bad Output:
http://example.com/index.php?route=something/special?a=1&b=2&c=3

This is quite a big system which contains a lot of similar (but different) scenarios. What is the best solution (one that does not rely on .htaccess) to resolve this? Ideally one which doesn't incur too many changes upon the system...
0
Comment
Question by:numberkruncher
[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
3 Comments
 
LVL 13

Author Comment

by:numberkruncher
ID: 34910677
One interesting point is that the bad URI resolves to the following PHP $_GET array:
array(
   'route' => 'something/special?a=1'
   'b' => '2',
   'c' => '3'
)

Open in new window

Obviously I can fix this by doing something like the following (with better error checking obviously):
$temp = explode('?', $_GET['route']);
$_GET['route'] = $temp[0];

$temp = explode('=', $temp[1]);
$_GET[$temp[0]] = $temp[1];

Open in new window


But will this cause problems? Especially with SEO?
0
 
LVL 20

Accepted Solution

by:
thehagman earned 500 total points
ID: 34911470
Why not use
echo myURI('/something/special','a=1&b=2&c=3')

Open in new window

and either define
myURI($path,$params) { return 'http://example.com' . $path . ($params? '?'.$params : '')

Open in new window

or
myURI($path,$params) { return 'http://example.com/index.php?route=' . $path . ($params? '&'.$params : '')

Open in new window

0
 
LVL 13

Author Closing Comment

by:numberkruncher
ID: 34912005
Thanks, that should do the trick.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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