Welcome to WebmasterWorld Guest from 54.226.246.160

Forum Moderators: bakedjake

Message Too Old, No Replies

Help with RewriteRule - mod_rewrite

A simple question. I've look through the manuals, but nothing

     

Soupisgoodfood

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

10+ Year Member



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 127.0.0.1] File does not exist: c:/mysite/getimage/RANDOM_NAME.jpg"

Please help me before I go insane.

Thanks,
Justin.

littleman

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

littleman

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Brett_Tabke

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

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

littleman

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Soupisgoodfood

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

10+ Year Member



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

Soupisgoodfood

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

10+ Year Member



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

gethan

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

WebmasterWorld Senior Member 10+ Year Member



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.

littleman

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

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Did Gethan approach do the trick?

Soupisgoodfood

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

10+ Year Member



Nope, still get 'bad request'.

gethan

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

WebmasterWorld Senior Member 10+ Year Member



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.

Soupisgoodfood

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

10+ Year Member



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.

gethan

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

WebmasterWorld Senior Member 10+ Year Member



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

Soupisgoodfood

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

10+ Year Member



File not found
"[Wed Feb 13 09:52:18 2002] [error] [client 127.0.0.1] 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.

Soupisgoodfood

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

10+ Year Member



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.

Thanks!

annefromuk

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

10+ Year Member



@Soupisgoodfood

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 :)

ergophobe

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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
G:/htdocs/some-dir/junk.php

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

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

G:/htdocs/some-dir/junk.php

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

G:/htdocs/junk.php

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

gethan

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

WebmasterWorld Senior Member 10+ Year Member



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.

Try:

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.

Gethan

ergophobe

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

/some-dir/some-dir/junk.php

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
127.0.0.1 - - [26/Apr/2002:10:00:49 -0700] "GET /ergophobia/climb2 HTTP/1.1" 400 293

where it want

[localhost...]

to map to
[localhost...]

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!

gethan

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

WebmasterWorld Senior Member 10+ Year Member



> 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.

[perl]
<html>
<head>
<title>Variables</title>
</head>
<body>
<h1>TEST SCRIPT</h1>
<?php
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>";
$array = $HTTP_SERVER_VARS;
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>";
$array = $HTTP_COOKIE_VARS;
foreach ($array as $key => $value) {
print "$key: $value<br>\n";
}
?>

</body>
</html>
[/perl]

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

Gethan

ergophobe

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

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Gethan,

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.

Cheers,

Tom

 

Featured Threads

Hot Threads This Week

Hot Threads This Month