views:

90

answers:

2

I'm running PHP on a shared Apache web server. I can edit the .htaccess file.

I'm trying to simulate a file file structure that is not actually there. For example, I would like for the URL: www.Stackoverflow.com/jimwiggly to actually display www.StackOverflow.com/index.php?name=jimwiggly I got halfway there by editing my .htaccess file as per the instructions in this post: http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure:

RewriteEngine on
RewriteRule ^jimwiggly$ index.php?name=jimwiggly

This works nicely insofar as the URL bar still displays www.Stackoverflow.com/jimwiggly and the correct page loads, however, all of my relative links remain unchanged. I could go back in and insert <?php echo $_GET['name'];?> before each link, but it seems like there might be a better way than that. Additionally, I suspect my whole approach might be off, should I be going about this differently?

+2  A: 

In order to accomplish more easily, and with more finesse, you could use a framework. A framework will allow you to use URL routing (you could also do it manually).

Check out the following SO questions/answers for some guidance on PHP frameworks.

Nate Pinchot
+5  A: 

I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.

In your htaccess use like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as

class URI
{
   var $uri;
   var $segments = array();

   function __construct()
   {
      $this->uri = $_SERVER['REQUEST_URI'];
      $this->segments = explode('/',$this->uri);
   }

   function getSegment($id,$default = false)
   {
      $id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
      return isset($this->segments[$id]) ? $this->segments[$id] : $default;
   }
}

Use like

http://mysite.com/posts/22/robert-pitt-shows-mvc-style-uri-access

$Uri = new URI();

echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'

Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param

Hope this helps you move forward with your application.

RobertPitt
+1 Or better yet use MVC ;).
Byron Whitlock
yea i would of explained that to him but seemed like he was already half way threw his application so just gave best possible answer without having to recode all application.
RobertPitt
@RobertPitt - Yeah, I put together this website back in 2004 and its evolved over time, if I had to do it over again, I'd use a framework. But starting from where it is now, something lower impact like this will work better. Thanks a lot.
DutrowLLC
@RobertPitt - Ok, so do I just use the "Uri" object to go back and edit all of the relative links?
DutrowLLC
The $Uri is used just like get but with a -> instead of an array so $_GET[0] is the same as $Uri->getSegment(0); the first item passed in the URI index.php?/first/second/third/etc/etc, i just made 1 small edit to the getSegment() Method
RobertPitt
@RobertPitt - Thats cool, so basically I still need to go back and edit all of my links, right?
DutrowLLC
@RobertPitt - I think "$this->url" should be "$this->uri" in "__construct()"
DutrowLLC
Yea your right in both of your right in both of your questions.
RobertPitt