Forum Moderators: phranque

Message Too Old, No Replies

Rewriting folder /file to folder/folder/file problem

         

isitreal

6:05 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm having problems with this rewrite rule, searched and couldn't find a similar question.

I need to redirect a group of .txt files from /folder1/file_name_a.txt to /folder1/folder2/file_name_a.txt

I tried the following rewrite rules and they aren't working:

RewriteCond %{REQUEST_URI} ^/folder1/([a-z_]+\.txt)
RewriteRule ^(.+)$ /folder1/folder2/$1 [L]

This does rewrite, but results in:

/folder1/folder2/folder1/file_name_a.txt

In other words, it's taking the whole rewrite condition string, as far as I can tell anyway, and placing it into $1, instead of the ([a-z_]+\.txt) string, which is the actual file name. I thought that what goes in the parenthesis is what is placed into $1, but obviously I'm once again not getting this.

I also tried this:

RewriteRule /folder1/([a-z_]+\.txt) /folder1/folder2/$1

which again, theoretically, should take anything containing a-z or _ plus the .txt extension and place it into $1. But it does nothing.

I've tried this many different ways, the first condition is the only one that appears to do anything at all. Any help on this would be appreciated, as always, thanks.

If I add this condition to the first rule:
RewriteCond %{REQUEST_URI} ^!/folder1/folder2/

as the first condition, in case there is a loop or something, the rewrite rule stops working, resulting in a 404 for /folder1/file_name_a.txt.

jdMorgan

7:22 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



isitreal,

Back-references to RewriteCond patterns use %1 through %9. Back-references to RewriteRule patterns use $1 - $9.


RewriteCond %{REQUEST_URI} ^/folder1/([a-z_]+\.txt)
RewriteRule ^(.+)$ /folder1/folde[b]r2/%1[/b] [L]

But that whole rule can be simplified to:

RewriteRule ^folder1/(.+\.txt)$ /folder1/folder2/$1 [L]

(RewriteRule does not "see" the "/" prefix on the URL-path in .htaccess context, so do not include it in your pattern.)

This code

 RewriteCond %{REQUEST_URI} ^!/folder1/folder2/ 

should read
 RewriteCond %{REQUEST_URI} [b]!^/[/b]folder1/folder2/

but it is not needed, because your original and substitution URL are sufficiently unique to prevent a loop.

Jim

isitreal

7:51 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Jim, thanks for the clarifications, I had to change this:

RewriteRule ^folder1/(.+\.txt)$ /folder1/folder2/$1 [L]

to this:

RewriteRule ^folder1/([a-z_]+\.txt)$ /folder1/folder2/$1 [L]

because while it worked for folder1/file_name_a.txt, for a file like folder1/file_name.txt it sent apache into some kind of loop of redirects til I get the redirect limit exceeded popup box. Reverting to [a-z_]+ fixed that.

It was that leading / that made it not work, I thought I'd tried removing that but I guess I didn't. Thanks again.

I think I might add an apache mod_rewrite configuration for idiots section to my site, I'm clearly qualified to write that, I keep tripping up on these really basic things.