homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
Forum Library, Charter, Moderators: bakedjake

Linux, Unix, and *nix like Operating Systems Forum

Help with RewriteRule - mod_rewrite
A simple question. I've look through the manuals, but nothing

 7:05 am on Feb 12, 2002 (gmt 0)

Hi. I want to send every request
from: C:\mysite\getimage\ANY_FILENAME_WHATSOEVER
to: C:\mysite\getimage\getimage.php

My web root is C:\mysite\

The htaccess file is sitting in C:\mysite\getimage\ht.access (yes '.' is in the wrong place, but win don't like files begining with '.''s and I've configued apache to use it.

In general. The site is working fine, the htaccess file it being read etc. But the code inside it isn't working.

I have tried the following:

RewriteEngine on
RewriteRule ^C:\\mysite\\getimage\\.$ C:\\mysite\\getimage\\getimage.php

RewriteEngine on
RewriteBase C:\\mysite\\getimage\\
RewriteRule ^.$ getimage.php

RewriteEngine on
RewriteBase /getimage
RewriteRule ^.$ getimage.php

And heaps of other variations. Sometime I get a syntax error etc. But most of the time, and for the above. When I make a request for [localhost...] I get this in the error log:
"[Tue Feb 12 19:54:23 2002] [error] [client] File does not exist: c:/mysite/getimage/RANDOM_NAME.jpg"

Please help me before I go insane.




 7:51 am on Feb 12, 2002 (gmt 0)

Brett has dome some work with apache and mod_rewrite on a windows, maybe we could grab his attention.


 7:55 am on Feb 12, 2002 (gmt 0)

I will probably move this to the 'website technology' forum eventually. You may find that forum more helpful in this case.


 7:59 am on Feb 12, 2002 (gmt 0)

Well, I using the exact same .htaccess here under redhat that I am at home under 98se/apache.

The only tricky part for me was making sure to use absolute paths in the .conf file. After that, it was identicle to linux for me. (same filename - everything - htaccess is even in unix format)


 8:09 am on Feb 12, 2002 (gmt 0)

With that in mind, maybe try this:
RewriteEngine on
RewriteBase /
RewriteRule ^$ getimage.php
RewriteRule (.*) getimage.php


 9:07 am on Feb 12, 2002 (gmt 0)

Thanks littleman. I tried that. But I got a 'bad request' error.
To the other replys: I don't think the problem with me being on a windows box. I think it's my script.


 9:10 am on Feb 12, 2002 (gmt 0)

...oh yeah. And sorry about posting in this forum. I must have missed the web technologies one.


 9:22 am on Feb 12, 2002 (gmt 0)

Littleman - you nearly had it :)

Place this in your .htaccess in your getimage dir (don't want to slow other requests down).

RewriteEngine on
RewriteBase /getimage/
RewriteRule ^(.*)$ getimage.php [T=application/x-httpd-cgi,L]

Best mod_rewrite cookbook - [engelschall.com...]

Lots of other options - once you get a simple example going you'll grok it.


 9:34 am on Feb 12, 2002 (gmt 0)

Did Gethan approach do the trick?


 10:47 am on Feb 12, 2002 (gmt 0)

Nope, still get 'bad request'.


 11:13 am on Feb 12, 2002 (gmt 0)

Your script does work right?

The application type can be changed to be php specific...

[T=application/x-httpd-php,L] - if you don't have type of cgi setup.


 12:01 pm on Feb 12, 2002 (gmt 0)

Nope.. still doesn't work

BTW. I had a look in the MIME types in the apache files, and there was no application/x-httpd-php, or the CGI one.

Well... It's not generating errors. But it's not exactly doing much either.


 12:59 pm on Feb 12, 2002 (gmt 0)

Ok .. put in a static html page that is served correctly eg. imagetest.html in your web root.

This is .htaccess in your getimage directory.

RewriteEngine on
RewriteBase /getimage/
RewriteRule ^(.*)$ /imagetest.html [L]

By testing with this you know that the problem is with your script and not your apache set up. I assume that mod_rewrite is in your version of apache - it is on my NT apache.

If it works its your script.

If it doesn't - theres a problem with your apache install - ,aybe not including mod_rewrite, start checking logs and type the error messages that you see into Google.

Good luck


 8:50 pm on Feb 12, 2002 (gmt 0)

File not found
"[Wed Feb 13 09:52:18 2002] [error] [client] File does not exist: c:/-= work =-/photoimage/site/www/test.html"

It seems to be missing the /getimage/ folder. tried it both /getimage/ and /

RewriteBase /getimage/
RewriteRule /getimage/^(.*)$ /getimage/test.html [L]

This works if I request test.html. But just returns a file not found if I type in garbage.

mod_rewrite seems to be on else I wouldn't have gotten bad agrument errors ealier on. And it's not commented out or anything in the httpd.conf file.


 9:06 pm on Feb 12, 2002 (gmt 0)

Opps! or got to put the test.html file in the root.

RewriteBase /getimage/
RewriteRule ^(.*)$ /getimage.php [L]

This code seems to be working now. Not sure why though. May be the RewriteBase rule only applies to the first part, and not the subsitution.



 5:40 pm on Feb 20, 2002 (gmt 0)


The reason why this:
RewriteBase /getimage/
RewriteRule /getimage/^(.*)$ /getimage/test.html [L]

Would not work because you were using Getimage twice.
Mod_Rewrite is looking to convert anything in /getimage/getimage/* to /getimage/getimage/test.html

Because you are now only using getimage in your rewritebase and not in the rules it is now working because it converts /getimage/* to /getimage/test.html

Well hopefully thats why, if not someone please correct me :)


 9:14 pm on Apr 25, 2002 (gmt 0)

I know this thread is supposed to be dead and it's on the wrong forum, but it is so appropriate to my question. Basically, I'm also trying to get the same thing to work.
So I've got

RewriteEngine on
RewriteBase /some-dir/
RewriteRule ^(.*) /junk.php [L]

This, however, ends up rewriting so that the actual file opened is not


as I want, but is one level up, that is


Everything I change to get this to go down to /some-dir breaks the script and I get a 404


 3:45 pm on Apr 26, 2002 (gmt 0)

Hi Ergophobe,

Welcome to the boards,

RewriteEngine on
RewriteBase /some-dir/
RewriteRule ^(.*) /junk.php [L]

Basically your / in front of the junk.php says get it from the root directory of your webserver.


RewriteEngine on
RewriteBase /some-dir/
RewriteRule ^(.*) /some-dir/junk.php [L]

or more generically

RewriteEngine on
RewriteBase /some-dir/
RewriteRule ^(.*) junk.php [L] # notice lack of slash.



 4:47 pm on Apr 26, 2002 (gmt 0)

Before I get too far, should I just move this to the Web Tech forum? It seems out of place here, but the actual post seemed to address my specific question.

>your / in front of the junk.php says get it from the root directory

That's what I figured, however, that's the only way I can get it to work. I've tried all sorts of combinations - absolute paths, with and without RewriteBase, etc etc.

>RewriteBase /some-dir/
>RewriteRule ^(.*) /some-dir/junk.php [L]

This just hangs up so I can't even get the error message for it, but RewriteBase should already be appending the /some-dir/ so the result should be


I think that's why this chokes.

>RewriteBase /some-dir/
>RewriteRule ^(.*) junk.php [L] # notice lack of slash.

This gives me a 400 error and doesn't seem to rewrite at all. There is no error log message for it, but it gives me - - [26/Apr/2002:10:00:49 -0700] "GET /ergophobia/climb2 HTTP/1.1" 400 293

where it want


to map to

I also tried

RewriteBase /
RewriteRule ^/some-dir/(.*) /some-dir/junk.php [L]

RewriteBase /some-dir/
RewriteRule ^(.*) ./junk.php [L]

# RewriteBase omitted
RewriteRule ^/some-dir/(.*) /somedir/junk.php [L]

It always just hangs up.

>Welcome to the boards

Thanks. Great set of forums. I got here by Google and since I don't feel comfortable asking a question on a forum I don't take part in, I cruised around. Learned a bit and, hopefully offered some useful advice in a couple of posts. I'll keep coming back and hopefully with more answers than questions!


 11:06 am on Apr 27, 2002 (gmt 0)

> should I just move this to the Web Tech forum? It seems out of place here

Hopefully a mod will spot this and move it across sometime :)

Ok now for some specifics, the rules I posted work fine on my system setup (Linux, Apache 1.3.22, with mod_rewrite and php 4.1). The RewriteBase only applies to the request and not the redirection. (I'll double check the docs for this sometime - but have never seen it work otherwise).

But something wierd is going on with your system - obviously without access to it the only help I can give is theoretical - but this is a little script that I find very useful for diagnosing redirect problems.

<h1>TEST SCRIPT</h1>
print "<p><b>Post Vars</b><br>";
$array = $HTTP_POST_VARS;
foreach ($array as $key => $value) {
print "$key: $value<br>\n";

print "<p><b>Get Vars</b><br>";
$array = $HTTP_GET_VARS;
foreach ($array as $key => $value) {
print "$key: $value<br>\n";
print "<p><b>Server Vars</b><br>";
foreach ($array as $key => $value) {
print "$key: $value<br>\n";

print "<p><b>Env Vars</b><br>";
$array = $HTTP_ENV_VARS;
foreach ($array as $key => $value) {
print "$key: $value<br>\n";

print "<p><b>Cookie Vars</b><br>";
foreach ($array as $key => $value) {
print "$key: $value<br>\n";


Make your redirect rules redirect to this script.

Take particular care and attention of the server vars in this case, particularly: anything starting with REDIRECT, REQUEST_URI, SCRIPT_FILENAME and SCRIPT_NAME.

I hope this lets you get to the bottom of this problem -- good luck



 5:18 pm on Apr 29, 2002 (gmt 0)


Thanks, but something is just weird beyond what I can figure out and running a script to show the variables doesn't really help because everything looks normal when it works, and, when it doesn't work, it gives a server error, page not found, infinite loop in Apache, something like thanks.

I think I'll just put up with redirect to server root on my Windows version, since that's actually how I want it to run on the Linux version anyway.



Global Options:
 top home search open messages active posts  

Home / Forums Index / Hardware and OS Related Technologies / Linux, Unix, and *nix like Operating Systems
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved