0

My SEF URLs are working fine on my local machine (WAMP Server) but when i upload it to server (Server API : CGI) it shows all the pages as 404 error. Here is the code i use :

1) .htaccess : 

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

2) index.php : 

<?php
function display_page($page) {
require_once 'Connections/cnntravels.php';
    /* get article */
    $sql = "SELECT contentid FROM tblcontents WHERE filename = '$page'";
    $res = mysql_query($sql);
    
    /* if not found, display 404 error */
    if (@mysql_num_rows($res) == 0) {
        require '404.php';
		exit;
    } else {    
        $row = mysql_fetch_object($res);
    }
    
    $_GET['contentid'] = $row->contentid;    // setup query string manually
    require 'page.php';    // call the 'real' script
}

/* get requested page */
$dir  = dirname($_SERVER['PHP_SELF']);
$page = basename(str_replace($dir, '', $_SERVER['REQUEST_URI']));
 
/* if empty display the homepage, otherwise display the page */
if (empty($page)) {
    require 'home.php';
} else {
    display_page($page);
}
?>

Please check my site www.thaitravelguides.com to see the problem and help me out.

Thanks in advance
Bob

3
Contributors
11
Replies
12
Views
7 Years
Discussion Span
Last Post by bobbymoir
0

do your pages actually exist. Would renaming them to lowercase and getting rid of the dash make any difference?

0

thanks ardav, but i have edited the filename column on the About Us page to 'about' lower case without any dash but the problem is the same, it doesn't make any difference. Please help

0
$sql = "SELECT contentid FROM tblcontents WHERE filename = '$page'";
    $res = mysql_query($sql);
 
    /* if not found, display 404 error */
    if (@mysql_num_rows($res) == 0) {
        require '404.php';
		exit;
    } else {    
        $row = mysql_fetch_object($res);
    }
 
    $_GET['contentid'] = $row->contentid;    // setup query string manually

OK, looks like you have a problem with the sql. Echo the sql statement to see what you get. If it looks sensible, paste it into phpmyadmin SQL box and run it - see what output you get. Something tells me the $page may be empty. Perhaps not.

I also don't understand why you'd want to set a $_GET variable. Doesn't make much sense to me.

Edited by diafol: n/a

0

Hi Ardav,

The sql query gives the expected result and also my script is working fine on my local machine. The problem is on the server only and i am thinking that the lines below is the problem :

$dir  = dirname($_SERVER['PHP_SELF']);
$page = basename(str_replace($dir, "", $_SERVER['REQUEST_URI']));

Hi mschroeder,

Thanks for replying and yes mod_rewrite is enabled on the server.

Thanking both you
Bob

0

Hi Ardav,
You were right. the problem is with mysql query but what could be the reason for it works fine on local machine and not on server ? please help me out.

N.B. : i have replaced

$dir  = dirname($_SERVER['PHP_SELF']);
$page = basename(str_replace($dir, "", $_SERVER['REQUEST_URI']));

to

$page = basename(str_replace('/', '', $_SERVER['REQUEST_URI']));
0

OK, we can't help you unless we know how you've stored your page info in the DB - is it just the filename (e.g. about.php) or is it absolute public root, (e.g. /content/about.php) or is it non-absolute (e.g. content/about.php) or is it full root (e.g. /home/fhlin270/c/example.com/user/htdocs/content/about.php)?

localhost and remote server *should* work the same way if your local testing server has the same basic 'home url format', e.g.
local = http://mysite.local/index.php
remote = http://www.example.com/index.php

Notice the single forward slash before the filename. If you have something like this:
local = http://localhost/mysite/index.php
remote = http://www.example.com/index.php

that can mess up up. In this case it's much easier to change your hosts and hosts-vhttpd.conf files if using windows - you can then dispense with the fuss of renaming root directories. If this is the issue, let us know so we can show you how to set up virtual hosts, so that you can use the first local example as opposed to localhost/mysite.

Perhaps I'm overcomplicating things here.

You can access files then by:

$_SERVER['DOCUMENT_ROOT'] . "/" . $file;

Edited by diafol: n/a

0

Hi Ardav,

I have just a column in my database as FILENAME (in lowercase). By the way, i have sorted out in the query to the real page i.e., page.php with something like

page.php?filename = basename(str_replace('/', '', $_SERVER['REQUEST_URI']));

but even though its working now i think this is not way. Can you suggest me the best way ? Or am i doing it right ?

Thanks
Bob

0

OK, I'd have done something simpler:

suppose the url = page.php?page=about

Why not just have the thing do this:

if(isset($_GET['page']) && file_exists(addslashes(htmlentities($_GET['page'] . ".php")))){
  include(addslashes(htmlentities($_GET['page'] . ".php")));
}else{
  include('home.php'); 
}

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.