Mod re write rules breaks php include() function

Hello forums

I have this strange problem with my mod rewrite rules. It used to be working without a hitch , then recently it is breaking my site most particularly the php include function.

example:

rule:
RewriteRule ^contents/([A-Za-z0-9-]+)/?$ index.php?page=contents&perma=$1 [NC,L]

url:
http://www.baguiotrader.com/contents/about-us-baguio-trader

it gives me these errors:

Warning: mysql_query() [function.mysql-query]: Access denied for user ‘nobody’@‘localhost’ (using password: NO) in /home/baguio/public_html/contents.php on line 4

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/baguio/public_html/contents.php on line 4

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/baguio/public_html/contents.php on line 5

Fatal error: Call to a member function set() on a non-object in /home/baguio/public_html/contents.php on line 10

But when I use this url it works fine:

http://www.baguiotrader.com/index.php?page=contents&perma=about-us-baguio-trader

Is there a server misconfiguration somewhere? I know htaccess is working, bec if it’s not then it should be throwing me 404 errors. Then again it used to be working …

Any ideas? Thanks

stone,

Obviously, it’s the redirection which is causing the problems. What about all the other “support files?” From what you’ve provided, I’d HAVE to guess that your include using a relative link and your redirection is changing the directory level (which it is). Use the <base> tag in your index.php script which is shown in my signature’s tutorial.

Regards,

DK

Hello DK,

this is whats on my index file:

define(__ROOT,$_SERVER['DOCUMENT_ROOT']);
define(__TEMPLATES,__ROOT.'/templates/');
define(__ENGINE_CORE, __ROOT.'/core/');
array_walk(glob(__ENGINE_CORE.'*.php'),create_function('$v,$i', 'return require_once($v);')); 

$page = $_GET['page'];

if ($page =='contents'){
include 'contents.php';
}else{
//do home page codes here
}

when you say base tag do you mean:


include '/contents.php';

Basically all files in the core directory is required once with this:

array_walk(glob(__ENGINE_CORE.‘*.php’),create_function(‘$v,$i’, ‘return require_once($v);’));

stonedeft,

Did you even look at the tutorial’s examples?

Regards,

DK

yes you said to use the base tag

<base http://www.site .com /external file />

however, how can I use the base tag with the include function?

Anyway it is so weired the error sudenly disappeared at least for me, but when my client opened the site the error is there eeeekk… I don’t know what to do I guess I’ll just have to abandon mod re writes for a while

stonedeft,

The <base> tag is used to tell the browser (where relative links are referenced to). On second thought, it should not matter to a PHP include() as that should be relative to the script’s location.

In other words, I don’t believe that there is anything wrong with your mod_rewrite.

Okay, after another look, consider:

RewriteRule ^/?contents/([-A-Za-z0-9]+)/?$ index.php?page=contents&perma=$1 [R=301,L]

Here, I’ve added the /? after the start anchor as you haven’t specified Apache 1 or 2 (this will work on both where your start anchor only version will only work with Apache 2).

I’d eliminate the optional trailing /, too, as that can cause “support file” problems (with relative links).

Finally, the No Case flag is already accounted for by your atom’s range definition. This flag is really (IMHO) only useful with the {HTTP_HOST} variable which is not case sensitive. I’ve replaced it with R=301 so you can TEST that the redirection is taking place (remove it when you’re satisfied it works).

Regards,

DK

thanks dklynn

It didn’t work. There is really something wrong with the server configuration of my hosting. Just now I checked and some items were missing on the database oh Jeez I hate that webhost

Anyway my rules works on my local host. I tried it on other webhosts and it also works. So I guess it’s time to switch host.

Thanks again you look great

Cheers!

Opps thats bad… Normally the error you are getting can be fixed by your web hosting provider.

Not with this webhosting. It will take them about 2 months to answer open tickets in their customer service area and usually they will take them another month blaming the client for this instead of fixing it…I’m not kidding if you want to know what webhost I’m talking about give me a PM and google a lot of their disgruntled cleints :x