Solved

Base URI and Pretty Links

Posted on 2011-02-16
3
348 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 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