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

Member Avatar for diafol

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

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

Member Avatar for diafol
$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.

Does the server you have uploaded your files to have mod_rewrite enabled for apache?

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

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']));
Member Avatar for diafol

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;

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

Member Avatar for diafol

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'); 
}

Hi Ardav,

Thanks, I shall try that.

Bob

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.