[web] mod_rewrite difficulties.

Started by
17 comments, last by Thevenin 17 years, 11 months ago
Quote:Original post by Sander
Also, on windows servers it's usually htaccess (without the .)

I wouldn't've known that. Nice catch. It's also worth noting that you can change this lookup path in the configuration.

If it still doesn't work, check the error logs. Additionally, I've needed to set RewriteBase / on one of my hosts whenever I use mod_rewrite--I still haven't figured out exactly why this is required. Either it's because of different Apache versions, or some obscure configuration settings.

@Thevenin:
I wasn't exactly clear when I specified the differences in setting directives between .htaccess and httpd.conf.

As the server configuration file can't understand relative paths, it must have a full path in the search expression (a / maps to the base directory). Rules placed in .htaccess can only understand relative paths to the executing directory--i.e., you must omit this forward slash in rules therein.

Therefore, a rule in httpd.conf consisting of
RewriteRule ^/(.*)$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [L]
becomes the following when placed in .htaccess:
RewriteRule ^(.*)$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [L]


Also, in the quote where you attempted my example, you missed the last directive. You might already understand the logic taking place behind it, but I'll put it forth anyhow.

Quote:RewriteRule ^cgi-bin/Fleurin.cgi/$ /cgi-bin/Fleurin.cgi?P=Home

This is simple. It re-maps any empty request to Fleurin.cgi to go to a default page (this shouldn't be handled here).
Quote:RewriteRule ^cgi-bin/Fleurin.cgi/forum/(.+)$ /cgi-bin/Fleurin.cgi?P=Thread&T=$1 [QSA,NC]

This takes a case-insensitive (specified by the NC flag) request to .../forum/ and substitutes it to the forum thread marked by the trailing value. The QSA flag makes sure that any additional query values are left intact.
Quote:RewriteRule ^cgi-bin/Fleurin.cgi/(.*)$ /cgi-bin/Fleurin.cgi?P=$1 [QSA]

Pretty much the same as the above rule, except that it controls page presentation.
Quote:RewriteRule ^([^.]*)$ /cgi-bin/Fleurin.cgi/$1 [N,QSA,L]

You might have noticed that, until now, the previous rules were all defined with a full prefix to the script. This takes any request that hasn't passed a previous rule, and conjoins with the script path. Then, the directives are re-examined with the newly modified request (consigned by the N flag).


The double parsing takes a slight [unnoticable] performance hit, but it's much cleaner. This should (I think) be negated if the rules are placed in the server configuration file.
Advertisement
Quote:Original post by silencer-
If it still doesn't work, check the error logs. Additionally, I've needed to set RewriteBase / on one of my hosts whenever I use mod_rewrite--I still haven't figured out exactly why this is required. Either it's because of different Apache versions, or some obscure configuration settings.


Log
[Fri Apr 21 02:57:15 2006] [error] [client 127.0.0.1] File does not exist: C:/Program Files/Apache Group/Apache2/htdocs/cgi-bin


cgi-bin is not located inside the htdocs folder, its located in the Apache2 folder. I guess 'problem solved'. [grin]

Quote:Original post by silencer-
I wasn't exactly clear when I specified the differences in setting directives between .htaccess and httpd.conf.

As the server configuration file can't understand relative paths, it must have a full path in the search expression (a / maps to the base directory). Rules placed in .htaccess can only understand relative paths to the executing directory--i.e., you must omit this forward slash in rules therein.

Therefore, a rule in httpd.conf consisting of
RewriteRule ^/(.*)$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [L]
becomes the following when placed in .htaccess:
RewriteRule ^(.*)$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [L]


Ahh, thanks for the clarification. [smile]

Quote:Original post by silencer-
Also, in the quote where you attempted my example, you missed the last directive.


I don't believe I did. If they are located in .htaccess, then after the first rewrite is complete (Which I stored in the htdocs folder), the remaining rewrites are now in the hands of the cgi-bin htaccess, and so they'd have to be delt with explicity by those last three rewrites.

Nevertheless, as I've put the rewrites back in the conf, I agree there should be all four in it.
The rewrite needs a little tweaking, I'll see if I can figure it out.
(cgi-bin is now located in htdocs)

Quote:File does not exist: C:/Program Files/Apache Group/Apache2/htdocs/cgi-bin/Fleurin.cgi/News


Should read Fleurin.cgi?News

Edit: Ok, I reversed your L's a bit...

RewriteEngine on
RewriteRule ^/cgi-bin/Fleurin.cgi/$ /cgi-bin/Fleurin.cgi?P=Home [L]
RewriteRule ^/cgi-bin/Fleurin.cgi/forum/(.*)$ /cgi-bin/Fleurin.cgi?P=Thread&T=$1 [QSA,NC,L]
RewriteRule ^/cgi-bin/Fleurin.cgi/(.*)$ /cgi-bin/Fleurin.cgi?P=$1 [QSA,L]
RewriteRule ^/([^.]*)$ /cgi-bin/Fleurin.cgi/$1 [N,QSA]

and now I get this when visiting 127.0.0.1/News



I'm not too sure what to think of it, since 127.0.0.1/cgi-bin/fleurin.cgi?p=news displays the page just fine, and doesn't prompt to download it. =/
Quote:Original post by Thevenin
Quote:Original post by silencer-
Also, in the quote where you attempted my example, you missed the last directive.


I don't believe I did. If they are located in .htaccess, then after the first rewrite is complete (Which I stored in the htdocs folder), the remaining rewrites are now in the hands of the cgi-bin htaccess, and so they'd have to be delt with explicity by those last three rewrites.

Ah, I hadn't noticed where you said you'd separated them [headshake].

Anyway, from what I recall, .htaccess is only executed upon user request, and mod_rewrite doesn't support .htaccess invocation during substitutions. I did a quick search and couldn't find anything to verify this, but I believe it to be correct.
Actually, I think that L deserves an omission entirely. I'm unsure as to what's happening there, however...

[edit:] Try placing
AddHandler cgi-script .cgi
Options +ExecCGI
in the root directory configuration. I'd forgotten that your CGI scripts were registered to a single directory.
Quote:Original post by silencer-
Actually, I think that L deserves an omission entirely. I'm unsure as to what's happening there, however...


I do believe both of these conditions are satisifed with the earlier one; I'm not too sure how Apache handles ambiguity.

^/cgi-bin/Fleurin.cgi/$
^/cgi-bin/Fleurin.cgi/(.*)$

Considering however, that "?P=" and "?P=Home" will take you to the same place in my CGI(C#) code, I think you are quite right. I also think that the

^/cgi-bin/Fleurin.cgi/$

can be omitted from the rewrite.



Quote:Original post by silencer-
[edit:] Try placing
AddHandler cgi-script .cgi
Options +ExecCGI
in the root directory configuration. I'd forgotten that your CGI scripts were registered to a single directory.


I was just thinking that... [grin]

Edit: Doh, I was soo hoping that would work... it didn't.
I was going to reply with something along the lines of a "I don't know" until I remembered that you'd had it working earlier.

I'd recommend scrapping the current structure, and reconstruct it sequentially. This way you can see just where it goes wrong.

For starters, try
RewriteRule ^/([^.]*)$ /cgi-bin/Fleurin.cgi?P=$1 [QSA,L]

For what it's worth, I tested the whole thing on two setups, and it performed correctly. I'm not sure what's so different here.
Quote:Original post by silencer-
try
RewriteRule ^/([^.]*)$ /cgi-bin/Fleurin.cgi?P=$1 [QSA,L]


That works! [grin]

Quote:Original post by silencer-
For what it's worth, I tested the whole thing on two setups, and it performed correctly. I'm not sure what's so different here.


Thanks for taking such interest in this. [smile]

[Edited by - Thevenin on April 21, 2006 4:27:04 AM]

This topic is closed to new replies.

Advertisement