Forum Moderators: phranque
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.
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]
RewriteRule ^folder1/(.+\.txt)$ /folder1/folder2/$1 [L]
This code
RewriteCond %{REQUEST_URI} ^!/folder1/folder2/ RewriteCond %{REQUEST_URI} [b]!^/[/b]folder1/folder2/ Jim
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.