0

I'm trying to expose a CGI file as my document root and web server. I do not want to expose the fact that the server is running a CGI script.

How can I map a URL http://host/index.cgi/ back to http://host/ in Apache2? I'm guessing it involves mod-rewrite, but I haven't finished grokking all the docs yet.

The following configuration is working, but I'm guessing there is a more complete solution:

RewriteEngine ON
Redirect /index.cgi/    /

2 Answers 2

1

but I'm guessing there is a more complete solution:

If you want to redirect requests for /index.cgi/ to /, then there's really no need for anything else. Note that RewriteEngine On is part of mod_rewrite, and the Redirect directive is part of mod_alias. You don't need the rewrite engine for the redirect to work. Also note that this is a 302 redirect, and it isn't a permanent one. You probably want to include either the 301 or permanent keywords in there:

Redirect 301 /index.cgi/ /

Additionally, the Redirect directive links two path nodes together, so any further paths appended to the source gets appended to the destination. So given the above, if you go to:

http://host/index.cgi/some/more/path.txt

The browser will get redirected to:

http://host/some/more/path.txt

If you don't want this, you can change the Redirect to a RedirectMatch and use a regular expression:

RedirectMatch 301 ^/index\.cgi/$ /
Sign up to request clarification or add additional context in comments.

1 Comment

Do know how you can hide the index.cgi in the URL, but have the path values passed to the script? Using the first Redirect breaks the script after the main page.
0

Correct solution using mod-rewrite.

  • Update CGI script to rewrite URLs to desired path. This means that URLS like index.cgi/path change to /app1/path or in my case, just /path. For my script there was an option called virutal-root that made a global change.

  • Then udpate Apache with the following directives. What it does, is internally rewrite all the URLs to include the CGI script, but ignore URLs going to the actual script.

    RewriteEngine ON
    RewriteCond %{REQUEST_URI}    !^/index.cgi
    RewriteRule ^(.*)$            /index.cgi/$1   [PT]
    
  • One sticking point, is that if the CGI script references any static files on the server, they need to be placed in a path that is also out of the rewrite rule. So for example, exclude another path, before the RewriteRule:

    RewriteCond %{REQUST_URI}     !^/_static
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.