Forum Moderators: phranque
I'm trying to write a RewriteRule that would redirect a .cgi to a .php, but apache does not like it.. How can I get arround to do that?
RewriteRule ^/cgi-bin/dir/scr1.cgi /var/www/html/dir2/scr2.php
I get an error saying that ExecCGI is not activated in the html directory.. if I add execcgi to the html directr, well it tries to execute the .php (and of coarse it does not start with #!/usr/bin/php.
How do I do that?
thnX
blakMonk
RewriteEngine on
RewriteRule ^/cgi-bin/dir/scr1.cgi http://www.domain.com/dir2/scr2.php [R=301,L]
I have several questions about what you wrote;
R=301 : what does it do ( where can I find the list of the letters and what they do)?
L => I know it's stops the treatment of the rules.
I'm looking for something transparent and with local files.
I managed to put rewrite rules for virtual hosts that get the file paths from a map file. But for some reason I cannot find the rule to redirect a cgi script to a pho script.
BlakMonk
In a per-directory override (.htaccess) file:
RewriteEngine on
RewriteBase /cgi-bin/dir
RewriteRule ^scr1\.cgi [example.com...] [R=301,L]
Or, in httpd.conf:
RewriteEngine on
RewriteRule ^/cgi-bin/dir/scr1\.cgi [example.com...] [R=301,L]
R=301 : what does it do...
Permanent redirection.
...( where can I find the list of the letters and what they do)?
RewriteRule [httpd.apache.org]
...and the numbers here:
ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt
\. to match a . char in the url, that's much safer, and more consequent. I don't suggest you to try to "fake" a cgi executable with a php file, although it might be working, but it is just weird, so I think if you don't have any arguments against redirect then you'd be better of to redirect only. Is there some html forms somewhere which are using this cgi to post data to (not with the GET method, that can be forwarded with the redirect)?
<add>
The L flag means; this is the last rule, stop processing any further rewrites. You really don't need it if you have only one, but you never know... Let's dig yourself into the mod_rewrite documentation, to find out more about this, I'll assure you that you'll be amazed on it's power.
</add>
I realy appreciate your help. But we are not quite there yet. I omited some information here they are.
I currently use mod_rewrte for virtual hosts. I use it in a .conf file that I include in httpd.conf. The rules use a map file that contains site and site_base_path.
everything works perfectly. But I'll add some L flags on some of the rules (thans for the link guys).
On of the scripts has a cgi script that is being accessed in two ways :
1- GET => blabla.com/cgi-bin/path/script1.cgi?var=value
2- POST => blabla.com/cgi-bin/path/script1.cgi
Now the problem :
I have rewriten the cgi script in php (way too heavy in perl, even with mod_perl). That and the fact that the modules I was abusing in perl did not all exist in php, I had to create the code myself, with lighter more specific code.
Now the users that access the script do not need to know and franckly most of them will take a awfull long time to do the url modification.
So my only option is to redirect/rewrite the blabla.com/cgi-bin/path/script1.cgi to blabla.com/path/script.php. All that in a way that the users keep accessing the .cgi script like they always did.
For that, you can see that I cannot use R=301 as it would return a code 301 to the application accessing the .cgi thus breaking whatever script that was calling my script.
Any ideas?
BlakMonk
It seems to me that your problem is that, when redirecting a .cgi to a .php, Apache is still treating the file as executable, and that is the problem.
Can you turn off ExecCGI in part of your directory space, and move the php file into that area?
Jim
I already tried adding ExecCGI in the html/path/ where the script.php resides.. well, apache tries to execute the .php not interprate it.
I cannot remove ExecCGI from where the cgi is because thare other scripts in that directory being used by clients.
I've tried a lot of stuff before posting here, still stuck ... :(
BlakMonk
RewriteRule ^/cgi-bin/test.cgi$ /php.php[L] /cgi-bin is a global (defined in httpd.conf) ScriptAlias for a directory, which haven't got any additional Options configured (not necessary since the ScripAlias does the job). The directory where the php file resides is a very common directory, does not have any options defined.
The most important is that I don't have any other rewriting for the directory, and not for the virtual server, this is the only one.
Which apache you are running?
Hi,
At this point, apache 1.3.27 on linux. But it will allso be running on apache 2.0.48.
I have several other rewrite rules for that set of virtuals. Here they are... maybe this will give you the same result I have. note, I did these 2 years ago, don't laugh too hard :)
Below you will see the content of the working rules . it uses a map file that contains lines like these:
www.blakmonk.com /virtuals/www.blakmonk.com
I stick the "RewriteRule ^/cgi-bin/dir/scr1.cgi /var/www/html/dir2/scr2.php" line in there, but no go...
------blak.conf
NameVirtualHost 192.168.15.100 NameVirtualHost 192.168.15.101
NameVirtualHost 192.168.15.102
<Directory /export/home/httpd/virtuels/*/cgi-bin>
Options FollowSymLinks ExecCGI
</Directory>
<VirtualHost 192.168.15.100 192.168.15.101 192.168.15.102 >
UseCanonicalName Off
RewriteEngine on
#- 4 hours 21600
CustomLog "¦/usr/sbin/rotatelogs /var/log/httpd/log.vblak 21600" vstuffed
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/etc/httpd/conf/vblak.map
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteRule ^/(.*)$ %1/html/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [T=application/x-httpd-cgi]
</VirtualHost>
-----
BlakMonk
---> Start HERE
RewriteCond %{SCRIPT_FILENAME} ^/cgi-bin/blak/blak.cgi
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/html/blak/blak.php$1 [T=application/x-httpd-php]
----> End HERE
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [T=application/x-httpd-cgi]
See, this i where I get confused.
BlakMonk
Unfortunately I'm not much into linux, so I do not realy understand why the substitution string are seems to be an absuolute file system path (in the RewriteMap file), but I think you can leave out the virtualhosting related part for this newly added RewriteRule. Hopefully someone who's much more experienced with linux will give you some more hint on this.
<edit>oops, seems i posted this one before i could have it completed</edit>
[...][b]
RewriteRule ^/cgi-bin/blak/blak.cgi$ /blak/blak.php [NC]
[/b]
RewriteCond %{[b]SCRIPT_FILENAME[/b]}!^/icons/
RewriteCond %{[b]SCRIPT_FILENAME[/b]}!^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/html/$1 [b][L][/b]RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/cgi-bin/(.*)$ %1/cgi-bin/$1 [T=application/x-httpd-cgi]