Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

mod_rewrite VS SSI VS your better idea

by derekstucki (Sexton)
on Aug 19, 2013 at 21:44 UTC ( #1050092=perlquestion: print w/replies, xml ) Need Help??
derekstucki has asked for the wisdom of the Perl Monks concerning the following question:

This isn't so much a question on how to use perl, but how to best implement something written in perl. I've been working on a website that serves every page via perl CGI, but I'd now like to hide the "/cgi-bin/" in the page addresses. I currently have using SSI in to serve, and so I was thinking of doing the same thing with all other pages, for example, serving /cgi-bin/products.cgi, etc, thus making a directory for each .cgi in /cgi-bin/ with an index.shtml in each directory. Obviously, this directory making nonsense would be automated by a perl script. :)

However, I've been recommended by a friend of a friend ( who may be a PHP or visual basic programmer for all I know ) that the better way to accomplish this is by using a .htaccess file directive using mod_rewrite. In order to do this, I think I'd have to do the same thing with directories with a .htaccess file in each directory for each .cgi, or have a line or two per .cgi file in the root .htaccess.

My question is this: which of these two methods is better, and why? Or do you know of a third method that works better than either of these?

Replies are listed 'Best First'.
Re: mod_rewrite VS SSI VS your better idea
by rnewsham (Deacon) on Aug 20, 2013 at 05:14 UTC

    There are various options, the simplest if it is available to you is reconfigure the server so you can run cgi scripts from the public directory. So you can just do Although I would recommend using mod rewrite to allow that to be accessed as which is tidier, preferred by search engines and allows you to change the script type without updating links and creating redirects. For example you could change home.cgi to .html, .shtml, .pl, .fcgi or switch to using Dancer and all your links to can remain unchanged.

    If the scripts must be kept in the cgi-bin then I would recommend using mod rewrite. You don't need a .htaccess file for each script you can use a single set of rules. Something like one of the two examples below should work. The first should allow you to use and the second These are just basic examples you would need to build upon these to do exactly what you want and protect against unwanted rewrites.

    RewriteEngine On RewriteCond %{REQUEST_URI} /*.cgi$ RewriteRule (.*) ../cgi-bin/$1 [l] or RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule (.*) ../cgi-bin/$1.cgi [l]
Re: mod_rewrite VS SSI VS your better idea (meaningful urls)
by Anonymous Monk on Aug 20, 2013 at 07:20 UTC
Re: mod_rewrite VS SSI VS your better idea
by derekstucki (Sexton) on Aug 21, 2013 at 17:40 UTC

    Thanks, Mewsham, for the input. After experimenting ( and even going to #httpd on freenode for help ) with mod_rewrite, and it not working*, I tried a ScriptAliasMatch directive in the virtual host file, and it seemed to do the trick.

    ScriptAliasMatch ^/foo/(\w+)$ /var/www/foo/cgi-bin/$1.cgi

    * When using the SSI method, 'get' arguments would work, but 'post' didn't seem to, although it might have been a different problem. The best I could get out of mod_rewrite was to display the correct script, but not run it.

    I'm considering this solved, but if someone else can post the mod_rewrite directive here that they have working in production to accomplish this ( or tell me what I was doing wrong with Mewsham's code ), I'd be happy to give it a try.

    Edit/Update: ScriptAliasMatch was working great on my test server, where I have root access, but when beginning to transfer the changes to our productions server, where I do not have root access, I found out that ScriptAliasMatch can only be in server config and virtual host files, and not in .htaccess files. On our production server, all config options must be in .htaccess files, so I went back to Rewrite. I was finally able to make it function properly with the following in my virtual host file:

    # the following will redirect to show the # output of the script previously accessed at #, assuming that direct access to # functions as desired <Directory "/var/www/foo/"> RewriteEngine On # enables Rewrite RewriteBase /foo/ # tells Rewrite to apply the following to # requests beginning with RewriteCond %{REQUEST_URI} !-d # rule applies if request does # not exist as a directory RewriteCond %{REQUEST_URI} !-f # rule applies if request does # not exist as a file RewriteRule ^(\w+)$ /foo/cgi-bin/$1.cgi [L] # perl-compatible # regex applies to # request string # following RewriteBase </Directory>

    In the .htaccess file, the Rewrite directives do not need to be in <Directory> tags. If this does not product the expected results, consider changing L for PT at the end of the RewriteRule.

    If one has root access to a server, ScriptAliasMatch is a simpler, and, I believe, more efficiently executed directive. If you must use .htaccess files, then Rewrite is your only option. Neither solution affects direct access to

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050092]
Approved by ww
Front-paged by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2018-06-18 09:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.