views:

31

answers:

2

EDIT: Having issues with the code below

Okay, I'm getting a page that says "The page isn't redirecting properly". This is if someone tries to access /files/protected/file.jpg. I'm trying to redirect it to /myfiles/file.jpg, but instead I get that error...

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /myfiles/
  RewriteRule ^(.*)$ $1 [L,R=301]
</IfModule>

Essentially, I'm trying to figure out how to add a rewrite condition that will retain part of the path. If a user tries, for example, to access a file within a directory, it redirects to another url with that filename as a parameter.

So, if a user visits: mysite.com/protected/file.pdf, it will redirect to mysite.com/okay/file.pdf

Is that something that I can use .htaccess for?

+1  A: 

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

You want the part about RewriteRule backreferences with regex groups referred to by $N.

For your example, I think this is what you've intended:

RewriteRule ^(mysite\.com/)protected(/.*) $1okay$2
Reinderien
Okay... Still having some issues... I'm editing my post above to show what I've done thus far...
KarmaKarmaKarma
+1  A: 

You need to put your .htaccess file in your site root. Then, assuming you want an external redirection, it should contain the following:

RewriteEngine On

RewriteRule ^files/protected/(.*)$ myfiles/$1 [R=301,L]

Your current rule always matches, so it performs the redirect an infinite number of times, and you end up with that error.

Tim Stone