URL not working off of home page

Hi

I am reading Social Networking 5 by PAKT publishing and am new at developing.

I am at Chapter 6. I have the home page (index) up and showing on my Xampp server - but none of the menu URLs are working. I suspect that this has something to do with my Apache server and have uncommented the url read / write mod and changed my Apache settings as per here:

http://stackoverflow.com/questions/17162214/htaccess-not-working-on-localhost-with-xampp

However, they are still not working. I suspect that this has something to do with Xampp and the url rewrite function. But, am a little bit out on a limb on that one (aka no idea why they are not working…)

Any suggestions out there? Happy to forward the code (but it is too lengthy with files) to post here.

thx
Karen

Do you have an .htaccess file?

Yes, found in the root of xampp

Openign it up: it reads:

AuthName "xampp user"
AuthType Basic
AuthUserFile "C:\xampp\security\xampp.users"
require valid-user

Okay, so I don’t think that is your problem, as you’d know if it was asking you to log on!

You say you have a home page showing up. What is the path to your home page? Are you accessing it through http://localhost?

Hi, thx for your time

Yes, and yes

I have imported the database database.php file into a new database which I have called called (brilliantly 'chapter6) successfully using a shell command from mysql root. (SOURCE C:/etc)

path:
c:/xampp/htdocs/xampp/test/social/W/chapter6

localhost:
http://localhost/xampp/test/social/W/chapter6/

Index.php is showing up - home page formatted - it falls over when I click any of the menu links, members, profil etc.

Error is as follows:

Object not found!



    The requested URL was not found on this server.

  

    The link on the
    referring
    page seems to be wrong or outdated. Please inform the author of
    that page
    about the error.

  



If you think this is a server error, please contact
the webmaster.



Error 404

  localhost

  Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8

How do you have the links in the page coded?

Hi

that’s a bit of a mouthfull. See below.

Also, the htaccess doc is NOT introduced in chapter7. Interestingly, I copied and pasted the chapter7 haccess file it into chapter 6 and ran the index on xampp from chapter 7 using whilst using the chapter6 database

The URLs when clicked all pointed to chapter6 url’s - so somehow the database is also involved in it. In otherwords, when using chapter6 database, the chapter7 home points to the chapter6 database urls and then falls over.

HTACESS file from chapter7 is as follows:

ErrorDocument 404 /index.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>

URL PROCESS IS AS BELOW
URL processing
Since we are using a single frontend controller, we need to process the incoming
URL, in particular the page $_GET variable, to work out how to handle the users
request. This is generally done by breaking the variable down in parts, separated
by a forward slash.

Manually setting the URL path is something we may need to do, so a simple setter
method is needed.
/**
* Set the URL path
* @param String the url path
*/
public function setURLPath($path)
{
$this->urlPath = $path;
}

The getURLData method processes the incoming URL, and breaks it down into parts,
building up an array of “URL bits”.

/**

  • Gets data from the current URL
  • @return void
    */
    public function getURLData()
    {
    $urldata = ( isset( $_GET[‘page’] ) ) ? $_GET[‘page’] : ‘’ ;
    $this->urlPath = $urldata;
    if( $urldata == ‘’ )
    {
    $this->urlBits = ‘’;
    $this->urlPath = ‘’;
    }
    else
    {
    $data = explode( ‘/’, $urldata );
    while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
    {
    array_shift( $data );
    }
    while ( !empty( $data ) && strlen( end( $data ) ) === 0)
    {
    array_pop($data);
    }
    $this->urlBits = $this->array_trim( $data );
    }
    }

The rest of our social networks code needs to access the URL bits to determine what
they need to do, so we need a suitable get method.

public function getURLBits()
{
return $this->urlBits;
}

Similarly, we may need to have easy access to a specific bit. For example, if the
request is friends/view/ID, the first bit would indicate that we use the friend’s
controller; the friends controller would then use a switch statement against the
second URL bit, to work out what it needs to do.

public function getURLBit( $whichBit )
{
return ( isset( $this->urlBits[ $whichBit ] ) ) ?
$this->urlBits[ $whichBit ] : 0 ;
}

Another getter we need is to get the URL path.

public function getURLPath()
{
return $this->urlPath;
}

If we need to generate a URL, for instance, to build a link, or redirect the user, we
can make this easier with a helper function, which takes an array or URL $bits, any
additional information to go in the query string of the URL, $qs, and if the URL is an
administrative URL, $admin, (if it is, then it appends the administration directory to
the URL).

public function buildURL( $bits, $qs, $admin )
{
$admin = ( $admin == 1 ) ? $this->registry->getSetting('admin_
folder') . '/' : '';
$the_rest = '';
foreach( $bits as $bit )
{
$the_rest .= $bit . '/';
}
$the_rest = ( $qs != '' ) ? $the_rest . '?&' .$qs : $the_rest;
return $this->registry->getSetting('siteurl') . $admin . $the_rest;
}

ps - perhaps I should just put the full chapter7 folders somewhere?

thx!
Karen

Also: as all of our code is passed through the Apache mod_rewrite, I do suspect that it has something to do with that. The files and their code have been on the web for five years, it probably works.

Most likely, there is something just wrong with my Apach. (My guess)

The Front Controller pattern
The Front Controller pattern is a single file, through which all requests are routed
(in our case, using Apache’s mod_rewrite). In our case, this will almost definitely
be the index.php file. This file will process the user’s request, and pass the request
to the appropriate controller.

sorry, this may be easier to read:

<?php

class urlprocessor {
    
    private $urlBits = array();
    private $urlPath;

    public function __construct( Registry $registry )
    {
        $this->registry = $registry;
    }
    
    /**
     * Set the URL path
     * @param String the url path
     */
    public function setURLPath($path)
    {
        $this->urlPath = $path;
    }
    
    /**
     * Gets data from the current URL
     * @return void
     */
    public function getURLData()
    {
        $urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
        $this->urlPath = $urldata;
        if( $urldata == '' )
        {
            $this->urlBits[] = '';
            $this->urlPath = '';
        }
        else
        {
            $data = explode( '/', $urldata );
            while ( !empty( $data ) && strlen( reset( $data ) ) === 0 ) 
            {
                array_shift( $data );
            }
            while ( !empty( $data ) && strlen( end( $data ) ) === 0) 
            {
                array_pop($data);
            }
            $this->urlBits = $this->array_trim( $data );
        }
    }
    
    public function getURLBits()
    {
        return $this->urlBits;
    }
    
    public function getURLBit( $whichBit )
    {
        return ( isset( $this->urlBits[ $whichBit ] ) ) ? $this->urlBits[ $whichBit ]  : 0 ;
    }
    
    public function getURLPath()
    {
        return $this->urlPath;
    }
    
    private function array_trim( $array ) 
    {
        while ( ! empty( $array ) && strlen( reset( $array ) ) === 0) 
        {
            array_shift( $array );
        }
        
        while ( !empty( $array ) && strlen( end( $array ) ) === 0) 
        {
            array_pop( $array );
        }
        
        return $array;
    }
    
    public function buildURL( $bits, $qs, $admin )
    {
        $admin = ( $admin == 1 ) ? $this->registry->getSetting('admin_folder') . '/' : '';
        $the_rest = '';
        foreach( $bits as $bit )
        {
            $the_rest .= $bit . '/';
        }
        $the_rest = ( $qs != '' ) ? $the_rest . '?&' .$qs : $the_rest;
        return $this->registry->getSetting('siteurl') . $admin . $the_rest;
        
    }
    
    
    
}
?>

I see the htaccess has an “if”

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>

So is the mod_rewrite.c module installed and enabled in the httpd.conf file?

Maybe it would work OK if you removed the surrounding “if” condition ?

BTW, testing for “if” IMHO is only needed for code that will be used by others when it can’t be known ahead of time what their set-up is like, once you know whether or not it’s there, having the code test for it all the time is a waste.

yes, it is not ‘hashed out’ and is now on.

Interestingly enough, the htaccess file disappears in later chapters. When I copy and paste the file into later chatpers, the index also no longer loads - i.e. the whole thing goes offline.

In either event,

LoadModule rewrite_module modules/mod_rewrite.so

is enabled in my apache. (Also, turned it on / off and rebooted Apache for good measure)

thx
Karen

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.