Solved

Base URI and Pretty Links

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.

733 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