开发者

.htaccess redirection and variables?

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 inste开发者_开发技巧ad 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?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜