3

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: 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?

Community
  • 1
  • 1
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258

1 Answers1

7

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
  • 56,863
  • 21
  • 114
  • 161
  • 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 Jun 29 '10 at 21:59
  • @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. – Chris Dutrow Jun 29 '10 at 22:03
  • @RobertPitt - Ok, so do I just use the "Uri" object to go back and edit all of the relative links? – Chris Dutrow Jun 29 '10 at 22:08
  • 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 Jun 29 '10 at 22:12
  • @RobertPitt - Thats cool, so basically I still need to go back and edit all of my links, right? – Chris Dutrow Jun 29 '10 at 22:49
  • @RobertPitt - I think "$this->url" should be "$this->uri" in "__construct()" – Chris Dutrow Jun 29 '10 at 23:44
  • Yea your right in both of your right in both of your questions. – RobertPitt Jun 30 '10 at 05:59