0

First time I've tried making pretty urls and this is my system, .htaccess maps everything back to index.php, index.php splits the URL into an array, then uses a case statement to decide how to proceed.

.htaccess:

RewriteBase /

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^.*$ ./index.php

index.php:

$request = $_SERVER['REQUEST_URI'];
#split the path by '/'
$params = explode("/", $request);
var_dump($params);
if(!isset($params[1]) || $params[1] == "") {
    require_once('inc/content_default.php');
} else if ($params[1] == "posthandler"){
    require_once('bin/posthandler.php');
} else {
    switch($params[1]) {
        case "dashboard":
            include('inc/content_active.php?ign=' . $params[2]);
        break;
    }
}

The issue is that when the url is: [domain]/dashboard/AviateX14, I get the error:

Warning: include() [function.include]: Failed opening '/inc/content_active.php?ign=AviateX14' for inclusion (include_path='.:/opt/php-5.3/pear') in /home/u502822836/public_html/index.php on line 92

The file does exist, the issue is with the code, I know this because I've tested by putting in include('index.php') and it still fails. The file does exist at inc/content_active.php.

What am I doing wrong guys? :(

EDIT: var_dump of $params is :

array(3) { [0]=> string(0) "" [1]=> string(9) "dashboard" [2]=> string(9) "AviateX14" } 
AviateX14
  • 760
  • 4
  • 18
  • 36

2 Answers2

1

You can't have query strings in a php include(), since that's not actually being requested. You'll have to fill in the parameters yourself in the server variables:

switch($params[1]) {
    case "dashboard":
        $_GET['ign']=$params[2];
        include('inc/content_active.php');
    break;
}
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks, that's got me to the page, but now the issue is that it isn't applying the css or html, presumably .htaccess is writing those urls back to the index.php page, any way around this? You can see what's happening here: http://lolstats.wc.lt/dashboard/AviateX14 – AviateX14 Jan 23 '15 at 15:23
  • @AviateX14 going to guess that your css etc are all linked using relative URL's (doesn't start with a `/`)? If so you need to change them to absolute URLs or add a relative URI base to the page headers: `` – Jon Lin Jan 23 '15 at 15:25
  • @AviateX14 http://stackoverflow.com/questions/27744603/css-js-and-images-do-not-display-with-pretty-url – Sumurai8 Jan 23 '15 at 15:27
1

You're asking PHP to look for a file called: inc/content_active.php?ign=AviateX14, including the ?ign=AviateX14 part.

One way around this would be to do:

$_GET['ign'] = $params[2];
include( 'inc/content_active.php' );

But it's not a very good practice to assign to $_GET; there are better things you could be doing.

Paul
  • 139,544
  • 27
  • 275
  • 264