Welcome to the Linux Foundation Forum!

Lab 8.2 - RewriteRule not working

CarloMartini Posts: 16
edited January 2019 in LFS211 Class Forum

I followed step-by-step (I'm on a Fedora machine) the provided solution, and after enabling the log for Rewrite Rules, it DOES seem to work, even if I'm not sure about that last message (' to get passed through to next API URI-to-filename handler'):

[Sat Jan 12 14:13:16.457004 2019] [rewrite:trace2] [pid 957:tid 140718237189888] mod_rewrite.c(482): [client ::1:46332] ::1 - - [localhost/sid#55d059c0fd20][rid#7ffb78008c00/initial] init rewrite engine with requested uri /foo/bar
[Sat Jan 12 14:13:16.457064 2019] [rewrite:trace3] [pid 957:tid 140718237189888] mod_rewrite.c(482): [client ::1:46332] ::1 - - [localhost/sid#55d059c0fd20][rid#7ffb78008c00/initial] applying pattern '^/foo/(.*)' to uri '/foo/bar'
[Sat Jan 12 14:13:16.457085 2019] [rewrite:trace2] [pid 957:tid 140718237189888] mod_rewrite.c(482): [client ::1:46332] ::1 - - [localhost/sid#55d059c0fd20][rid#7ffb78008c00/initial] rewrite '/foo/bar' -> '/scripts/foo.cgi?bar'
[Sat Jan 12 14:13:16.457098 2019] [rewrite:trace3] [pid 957:tid 140718237189888] mod_rewrite.c(482): [client ::1:46332] ::1 - - [localhost/sid#55d059c0fd20][rid#7ffb78008c00/initial] split uri=/scripts/foo.cgi?bar -> uri=/scripts/foo.cgi, args=bar
[Sat Jan 12 14:13:16.457110 2019] [rewrite:trace2] [pid 957:tid 140718237189888] mod_rewrite.c(482): [client ::1:46332] ::1 - - [localhost/sid#55d059c0fd20][rid#7ffb78008c00/initial] forcing '/scripts/foo.cgi' to get passed through to next API URI-to-filename handler

Alas, when I visit http://localhost/foo/bar, the address is still the same (I also tried with wget/curl, so it doesn't seem a browser issue)


  • lee42x
    lee42x Posts: 380

    Hi Carlo ! Did the last step in exercise 8.1 work?
    It was :
    Restart Apache and test your new script. http://localhost/scripts/foo.cgi?bar
    If the exercise 8.1 functions then it is a rewrite issue if it did not then the foo.cgi script needs attention.

  • CarloMartini

    Yes, it did work. There was only a misconception on my part, sorry!

    The redirection was supposed to be "transparent" and that's exactly what's happening. I see the URL http://localhost/foo/bar instead of the URL http://localhost/scripts/foo.cgi?bar

    I missed the "transparent" part and for some reason I was convinced I should have been seeing the URL change to http://localhost/scripts/foo.cgi?bar.

    Sorry and thanks for your time.

  • AlexeyBY
    AlexeyBY Posts: 56

    I use CentOS
    Lab 8.1 is Ok. It works.
    Lab 8.2 is not working. --> The requested URL /scripts/foo/bar was not found on this server.

    I've done the virtualhost lab from the previous chapter. Probably the root of the reason is the file namevhost.conf.
    I don't understand how the file namevhost.conf must be corrected. How can I find _default_namevhost section ?


  • lee42x
    lee42x Posts: 380

    Hi Alexey,
    There are sample configuration files in the SOLUTIONS directories that should help. In the SOLUTIONS/s_07 directory is a file "namevhost.conf " with the "default " server stanza included.
    Let me know if this helps.

  • gairsty
    gairsty Posts: 26

    Hi. I am also having issues with this lab.
    (Ubuntu 18)
    After following the instructions, all I get is the, "Not Found. The requested URL was not found on this server." message when trying to access http://localhost/foo/bar

    It's not particularly clear where the default stanza is in the namevhost.conf file. (I tried putting them most places in the file..)
    Also, on an Ubuntu system does one create the /etc/apache2/sites-enabled/rewrite.conf file as well as adding the rewrite commands to the 000-default.conf file? I think I've tried most variations, with no success.

  • gairsty
    gairsty Posts: 26

    I have now tried this in ubuntu20 and it also fails at 8.2.

    Anyone have any ideas? I thought it was maybe to do with the actual RewriteRule given.
    for rewrite.conf it's given as, RewriteRule \ˆ/foo/(.*) /scripts/foo.cgi?$1 [L,PT]
    But where it's to be added to the 000-default.conf file it's given as, RewriteRule ˆ/foo/(.*) /scripts/foo.cgi?$1 [L,PT]
    There's a backslash that I think is superfluous in the first instance.

    Then I looked again at http://httpd.apache.org/docs/current/rewrite/intro.html and all their examples have double quotes around each section, RewriteRule "ˆ/foo/(.*)" "/scripts/foo.cgi?$1" [L,PT]

    Having been through pretty much every permutation of these with no success, I await insight from a greater mind!!

    Please, this is incredibly frustrating!

  • lee42x
    lee42x Posts: 380
    edited July 2020

    I re-tested lab 8.2. It worked on Ubuntu 20.04 as described. Lets see if we can get yours going.

    Quotes vs backslash, either will work, it is just coding style.

    The three lines to be added to the 000-default.conf file (Debian and buddies) must be INSIDE the last "VirtualHost" stanza.

    My file looks like:
    root@ubuntu:/etc/apache2/sites-enabled# tail 000-default.conf

    RewriteEngine on
    RewriteOptions inherit
    RewriteRule ^/foo/(.*) /scripts/foo.cgi?$1 [L,PT]

    Verify apache can see the cgi directoy, here is my test using lynx :

    student@ubuntu:~/LFT$ lynx -dump http://localhost/scripts/foo.cgi?bar

    Content-type: text/plain

    File is bar

    Verify the rewrite works:

    student@ubuntu:~/LFT$ lynx -dump http://localhost/foo/bar

    Content-type: text/plain

    File is bar

    I hope this helps. Lee

    NOTE: the formatting of the stanza is messed up in this editor, screen shot included.

  • gairsty
    gairsty Posts: 26

    Hi Lee.
    Unfortunately this hasn't fixed the problem.

    Here's a screenshot of what I have, including the contents of rewrite.conf, namevhost.conf and testing of namevhost.


  • lee42x
    lee42x Posts: 380

    Well that is interesting.
    The three re-write directives should be in 000-default.conf or rewrite.conf, not both. On Ubuntu I have less issues if I modify 000-default.conf. What you have there looks fine, the first lynx command shows the cgi is working so it is just the rewrite. The re-write needs only two additional things, the configuration directives which look fine and the rewrite apache2 module.

    Can you please check your enabled modules against mine? See below :

  • lee42x
    lee42x Posts: 380

    Let's trace the rewrite and see if it is trying at all.

    • In /etc/apache2/apache.conf locate "LogLevel" directive.
    • Change LogLevel to "LogLevel alert rewrite:trace4"
    • restart apache2
    • test url with lynx
    • use grep to show the rewrite messages "grep rewrite /var/log/apache2/error.log "

    Below is a working example for you to use as a comparison.

  • gairsty
    gairsty Posts: 26

    Hi Lee.

    So I removed any mention of Rewrite from the namevhost.conf file.
    I removed the rewrite.conf file entirely.
    I added the cgid.conf and cgid.load mods to my mods-enabled directory and removed the cgi.load mod that was there (as per the lab instructions). My mods directory now looks like this,

    Only the 000-default.conf file has any mention of the rewrite rules.

    It still didn't work.

    Here's a snippet from the error.log,

    It's definitely not the same as yours. For some reason the IP addresses look like they're IPv6...
    But more interesting is in how the "applying pattern..." step is definitely wrong.

    instead of, applying pattern '^/foo/(.*)
    I have, applying pattern '\xcb\x86/foo/(.*)'

    hmmm, that rings a bell...

    So instead of selecting the text in the browser and middle-buttoning it into vim (to reduce the chance of typos), I typed it in and it worked.

    On the lab, ˆ is used instead of ^.

    Thanks for your time on this Lee, the rewrite:trace was invaluable.

    I think it needs to be clearer in the lab that rewrite.conf doesn't work in Ubuntu and shouldn't be added.


Upcoming Training