[web] mod_rewrite difficulties.

Started by
17 comments, last by Thevenin 17 years, 12 months ago
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]
Advertisement
You are looking for "mod_rewrite". Here is a cheat sheet. A google search should provide you with anything else you are looking for
RewriteRule ^news$ cgi-bin/Fleurin.cgi?P=News [NC,L]
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?
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 onRewriteRule ^/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]
This is much less ugly. Hopefully it works.

RewriteEngine onRewriteRule ^/cgi-bin/Fleurin.cgi/$ /cgi-bin/Fleurin.cgi?P=HomeRewriteRule ^/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]
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.".
  • 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?
    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 .)

    <hr />
    Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

    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

    This topic is closed to new replies.

    Advertisement