Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite .htaccess, what is wrong here?

checking for extra folders?

         

caspita

7:47 pm on Apr 9, 2004 (gmt 0)

10+ Year Member



Hi all,

I was looking thru the forum for something similar but I didn't see anything and apache is not my best skill :-(

Here is the case:

1. Ussing Apache for windows in my own pc, for local development.

2. .htaccess
============

RewriteEngine on
RewriteRule ^(.*).html /gp4v2/index.php?html_page=$1 [L]

3. php script (this is not the issue)
=====================================

<?
$html_page = (isset($_GET['html_page']))? $_GET['html_page'] : "index";

echo "html_page=$html_page";

switch ($html_page) {
case "index":$html_tpl = "tpl/index.tpl";
break;
default:$html_tpl = "tpl/index.tpl";
}

$html_code = file_get_contents($html_tpl);

echo $html_code;
?>

What I'm trying to do is the redirect every request for an .html page to a single .php script that will create the html code according to the url parsing.

In this example:

1. If I type [localhost...]

It works fine, by default I'm loading a file tpl/index.tpl and the .css file is being parsed.

2. If I type [localhost...]

It also works fine, again defaul file is showed and the .css files gets parsed.

3. But when I type [localhost...]

It partially works.. the php script is being executed (I know that because I'm displaying the value I got in the URL parameter), I can see the html code, but the .css is not parsed and in addition I can see an error in the apache error.log file, this is the error:

[Fri Apr 09 15:42:17 2004] [error] [client 127.0.0.1] File does not exist: C:/Program Files/Apache Group/Apache2/htdocs/gp4v2/xys, referer: [localhost...]

Looks like apache is looking for the non existent folder "xys", because it is in the URL, I'd like to avoid this because I'm ready to parse the whole string in the URL so the '/' won't be an issue for me in the script, but looks like apache is stoping before to look for the .css file.

Any idea what is wrong here?

Thanks a lot for your support.

Carlos.

caspita

8:14 pm on Apr 9, 2004 (gmt 0)

10+ Year Member



Some more info:

Checking into the access.log from the apache I found this:

127.0.0.1 - - [09/Apr/2004:16:11:53 -0400] "GET /gp4v2/xys/style.css HTTP/1.1" 404 293

Which confirms that it is looking for the .css file in a folder that does not exist, intead of look for it in the root folder.

But.. still trying to sort it out :-(

Anyone?

Thaks again.

caspita

8:19 pm on Apr 9, 2004 (gmt 0)

10+ Year Member



Added absolute path for the href css tag .. it is working now ;-)

Anyway .. is there no way to avoid the mod_rewite to relocate the root directory?

Carlos.

jdMorgan

1:12 am on Apr 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



caspita,

This all really depends on how much of the requested URL-path you want to pass to the script in the query string. The way you have written it, the entire path is passed, including subdirectory names.

With a minor correction this is what you are using now:


RewriteEngine on
RewriteRule ^(.*)\.html$ /gp4v2/index.php?html_page=$1 [L]

If you want to avoid passing the subdirectory path along with the page name, try:

RewriteEngine on
RewriteRule ([^/.]+)\.html$ /gp4v2/index.php?html_page=$1 [L]

This will grab one or more characters preceding ".html" except for "/" and literal "." characters. The effect is to grab anything after the last slash in the URL preceding ".html".

So, with your code, requesting [localhost...] will result in a query string value of "?html_page=xyz/xyzw", while with the modified code, the query string will be "?html_page=xyzw".

Jim

caspita

9:47 pm on Apr 12, 2004 (gmt 0)

10+ Year Member



Hi Jim,

That's correct, you examples are what I can see and is fine becasue each word in between the '/' will be a parameter for me, and I'm using a parser function to separate them. I can have multiple parameters acording to the structure of my page then the way is working is perfect.

The question is: why the root folder is being moved? I'mean, if I have a page like [localhost...] I'll get abc/xyz in the parameter, that is fine, but then every reference from my html code, 'like href=style.css' will start under 'abc' (looking the file like 'abc/style.css'), directory which does not exist, I have to use explicit reference like 'href=/gp4v2/style.css'.

I don't know if you get my point... do you?

Thanks

CS.

jdMorgan

10:28 pm on Apr 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is always the problem when passing relative path parameters to a script from pages at different subdirectory levels that the script no longer 'knows' what the passed path parameter is relative to. So, relative paths should always be specified with the Web root as the 'base' to avoid this problem. That is, the URIs you put in on-page links should aways be <a href="/somedir/somefile.html"> and not "<a href="somedir/somefile.html">

You may want to try removing the leading slash from the RewriteRule substitution - I believe I've seen that fix posted here before in a previous thread (but I can't find it). :(

Jim

caspita

2:20 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



Thanks Jim,

That means that it is an issue that needs to be workerd out as you are pointing .. it is what I'm doing now. So basically is nothing wrong with my mod_rewrite rules nor my appache config which is what I was thinking.

The only think I don't like of this is that while I'm working on my own pc I have an extra folder which is the root for my website (like ... c:\Prog....\htdocs\mywebsite\..) so I need to use the references like '/mywebsite/style.css' and once I deploy to my hosting server \mywebsite\ exists over there, but it is the root for the addon domain wwWebmasterWorldebsite.com, then I have to take out the '/mywbesite' and use the references like '/style.css' only .. but I think I'll be able to handle that using some extra php code.

or is there some way I can configure a 'site' in the apache for my own pc indicating the root folder is (c:\prog....\htdocs\mywebsite\), so when I use a reference like '/style.css' it will go back to that folder and not to the \htdocs\ default folder? I'll try to read about that a little more, that way I can avoid the extra php code which could be a little overload.

Thanks again for all your support.

Carlos.

jdMorgan

3:22 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Carlos,

You should be able to change the definition of Document_Root in the httpd.conf file on your PC to add the extra subfolder layer.

Jim

caspita

6:22 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



Thanks Jim :-)

But what if I have more that one folder, each one with a different website?

like [localhost...] [localhost...] and I use a .htaccess for each folder?

When I publish that in my hosting /site1/ is going to www.site1.com and /site2/ is going to www.site2.com, then the 'root' for each one will be '/' so for the case of the .css file which is in the root I'll use for both the reference like '/style.css', while when I'm on my own PC I need to use '/site1/style.css' and '/site2/style.css' .. this is just the case with one reference, but when you have many to edit the references is a pain :-(.

I have been playing with the <VirtualHost> and the Alias on my PC trying to get the same behavior but no luck so far :-(

Thanks,
Carlos.