Solved

Base URI and Pretty Links

Posted on 2011-02-16
3
317 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, that should do the trick.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

771 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

11 Experts available now in Live!

Get 1:1 Help Now