Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

mod_rewrite VS SSI VS your better idea

by derekstucki (Acolyte)
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 www.mysite.com/ using SSI in www.mysite.com/index.shtml to serve www.mysite.com/cgi-bin/home.cgi, and so I was thinking of doing the same thing with all other pages, for example, www.mysite.com/products/index.shtml 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?

Comment on mod_rewrite VS SSI VS your better idea
Re: mod_rewrite VS SSI VS your better idea
by rnewsham (Hermit) 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 www.mysite.com/home.cgi. Although I would recommend using mod rewrite to allow that to be accessed as www.mysite.com/home 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 www.mysite.com/home 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 www.mysite.com/home.cgi and the second www.mysite.com/home. 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 (Acolyte) 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 www.mysite.com/foo/bar to show the # output of the script previously accessed at # www.mysite.com/foo/cgi-bin/bar.cgi, assuming that direct access to # www.mysite.com/foo/cgi-bin/bar.cgi functions as desired <Directory "/var/www/foo/"> RewriteEngine On # enables Rewrite RewriteBase /foo/ # tells Rewrite to apply the following to # requests beginning with www.mysite.com/foo/ 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 www.mysite.com/foo/cgi-bin/bar.cgi.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050092]
Approved by ww
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (19)
As of 2014-07-11 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (231 votes), past polls