1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mod_rewrite rule with original PATH/QS

Discussion in 'Apache' started by yha, Jun 24, 2019.

  1. #1
    Hello,

    Please excuse my ignorance, but I cannot seem to get a simple rewrite rule to work the way I want. Basically, I'm trying to use friendly URLs for my blog. Actual blog lives in a single CGI file (blog.cgi, located at /blog/blog.cgi from the root). What I'm trying to accomplish is this: any hits coming to h t t p s : / / w w w . m y d o m a i n . c o m / b l o g / (with or without the trailing slash) to be routed to to blog.cgi, including proper handling of PATH_INFO and QUERY_STRING. e.g.:

    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g / -> /home/yha/public_html/blog/blog.cgi, PATH_INFO='', QUERY_STRING=''
    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g -> /home/yha/public_html/blog/blog.cgi, PATH_INFO='', QUERY_STRING=''
    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g s l k f -> 404
    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g / 1 -> /home/yha/public_html/blog/blog.cgi, PATH_INFO='/1', QUERY_STRING=''
    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g / 2 0 1 1 / 7 / 2 4 -> /home/yha/public_html/blog/blog.cgi, PATH_INFO='/2011/7/24', QUERY_STRING=''
    - h t t p s : / / w w w . m y d o m a i n . c o m / b l o g / 2 0 1 1 / 7 / 2 4 & m y p a r a m = 1 -> /home/yha/public_html/blog/blog.cgi, PATH_INFO='/2011/7/24', QUERY_STRING='myparam=1'

    I had several attempts at this, but never got it to work exactly like the examples above. This is the closest I got to, placing the following in .htaccess in my document root (/home/yha/public_html):

    RewriteEngine On
    RewriteRule "^/blog(.*)$" "/blog/blog.cgi" [H=cgi-script,NC,L,E=PATH_INFO:$1]

    The pattern, theoretically, should match any URL beginning with /blog followed by any number of characters, and have that captured in $1, which I set to PATH_INFO with the E flag. What I get is a 403: "You don't have permission to access the requested directory. There is either no index document or the directory is read-protected.". If I remove the rewrite rule and access /blog/blog.cgi it works just fine, so I know it's not a permissions issue.

    Can someone tell me what it is that I'm doing wrong? Thanks.

    P.S. Sorry for the extra spaces in the URLs, but the forums won't let me post unless all URLs are valid and return a response, which is kinda annoying given these are just examples to get my point across.
     
    yha, Jun 24, 2019 IP