• Advertisement
Sign in to follow this  

[web] mod_rewrite difficulties.

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had this figured out a long whiles ago, but I completely forget how to do it. I'm trying to mod_rewrite the following via Apache's mod_rewrite mod. (Without showing the changes in the client's URL bar)
http://127.0.0.1 to become
   http://127.0.0.1/cgi-bin/Fleurin.cgi?P=Home

http://127.0.0.1/Forum to become
   http://127.0.0.1/cgi-bin/Fleurin.cgi?P=Forum


http://127.0.0.1/Forum/3 to become
   http://127.0.0.1/cgi-bin/Fleurin.cgi?P=Thread&T=3
All I can come up with so far is this..
RewriteEngine on
RewriteRule ^/News /cgi-bin/Fleurin.cgi?P=News [nc]
... which doesn't work at all. =/ [Edited by - Thevenin on April 20, 2006 3:40:23 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
RewriteRule ^news$ cgi-bin/Fleurin.cgi?P=News [NC,L]

Share this post


Link to post
Share on other sites
I've tried a bunch of combinations now, and the only one that works is providing the full URL (http://127.0.0.1/cgi-bin/Fleurin.cgi?P=) for the redirection.

eg...


Doesn't Work
RewriteRule ^news$ cgi-bin/Fleurin.cgi?P=News [NC,L]

Works
RewriteRule ^/news$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [NC,L]

Any ideas?

Share this post


Link to post
Share on other sites
Sorry, I assumed that you were using .htaccess for the directives (which you should, for ease of portability).

Redirections within the server configuration file require that paths be absolute. Directory configuration, on the other hand, allows relative paths.

As a side note, I'd only glanced over your first post. What you're trying to do (encapsulate all URL requests) is extremely difficult to manage. Optimally, instead of hiding the user request from the script, it should be supporting its own URL format specification (as it is currently, but with the ugly URLs).

Nevertheless, it is possible to implement a limited redirect sequence.

RewriteEngine on

RewriteRule ^/forum/(.*)$ /cgi-bin/Fleurin.cgi?P=Thread&T=$1 [QSA,NC]
# Should be caught in the dispatcher (change the last rule's plus sign to an asterisk).
RewriteRule ^/$ /cgi-bin/Fleurin.cgi?P=Home
# Rather than explicitly setting each requestable page, allow the script to determine the request validity.
RewriteRule ^/([^.]+)$ /cgi-bin/Fleurin.cgi?P=$1 [QSA,L]

Share this post


Link to post
Share on other sites
This is much less ugly. Hopefully it works.

RewriteEngine on

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

RewriteRule ^/([^.]*)$ /cgi-bin/Fleurin.cgi/$1 [N,QSA,L]

Share this post


Link to post
Share on other sites
Adhering to your advice (somewhat), I...

  • Configured my apache conf to 'AllowOverride All'
  • Created a file in my htdocs directory called ".htaccess", inside it contains...

    Quote:
    RewriteEngine on

    RewriteRule ^/([^.]*)$ /cgi-bin/Fleurin.cgi/$1 [N,QSA,L]


  • I browsed to my cgi-bin folder and added the file ".htaccess" to it, inside this file contains...

    Quote:

    RewriteEngine on

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


  • Restarted Apache, browsed to "127.0.0.1/home", and got "The requested URL /home was not found on this server.".

    Share this post


    Link to post
    Share on other sites
    Heck even putting this..

    Quote:
    RewriteRule ^(.*)$ http://127.0.0.1/cgi-bin/Fleurin.cgi?P=News [L]


    In my htdocs (My home directory) doesn't even work. Am I missing the bigger picture?

    Share this post


    Link to post
    Share on other sites
    Are .htaccess files allowed in your apache? Some apache packages ship with .htaccess functionality turned off. Also, on windows servers it's usually htaccess (without the .)

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by Sander
    Are .htaccess files allowed in your apache? Some apache packages ship with .htaccess functionality turned off.


    Apache 2.0.55(Win32); downloaded from their homepage.

    Quote:
    Original post by Sander
    Also, on windows servers it's usually htaccess (without the .)


    AccessFileName .htaccess

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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. =/

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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.



    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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.

    Share this post


    Link to post
    Share on other sites
    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]

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement