Forum Moderators: phranque

Message Too Old, No Replies

Redirection problem with multiple files

Redirect multiple files (but not all of them) from a directory

         

cookie2

1:27 am on Nov 20, 2009 (gmt 0)

10+ Year Member



I am not very experienced in redirection. I can do the basics like redirecting an entire website or an entire directory but I need something different this time and am at a loss how to proceed.

I need to redirect all files in a directory that begin with "rec_" (minus the quotes) to a new directory while leaving all the other files in the old directory alone.

I tried several combinations the last of which was
Redirect permanent ^/abc/rec_(.*)$ http://example.com/abc/new/rec_(.*)
but with no success.

I can write the individual files to redirect fine:
Redirect permanent /abc/rec_file.htm http://example.com/abc/new/rec_ched.htm

What do I need to do to come up with a single entry that will save me writing hundreds of individual htaccess entries?

[edited by: jdMorgan at 2:52 am (utc) on Nov. 20, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:51 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See Apache mod_alias RedirectMatch directive. RedirectMatch uses regular-expression pattern-matching instead of the prefix-matching used by the Redirect directive.

If RedirectMatch won't do everything you need, then see Apache mod_rewrite. It's more powerful, but with a steeper learning curve.

Jim

cookie2

2:53 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



I have read the RedirectMatch and once again, the examples are for entire directories or for types of files within the directory (i.e. .gif to .jpg). I still don't understand how to match requests that begin with rec_ only.

jdMorgan

3:17 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like:

RedirectMatch 301 ^/abc/rec_(.+)$ http://example.com/abc/new/rec_$1

Jim

g1smd

4:25 pm on Nov 20, 2009 (gmt 0)

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



Have you moved files to a new folder on your server?

If so, it is possible for them to still retain the old URL.

Are you wanting new URLs for the content? If so, why?

You can give new URLs to the content whether or not you physically move the files around inside your server.

cookie2

12:43 am on Nov 21, 2009 (gmt 0)

10+ Year Member



Thank you Jim. My main mistake was using (.*) instead of (.+). Couldn't get that to work because I misunderstood the way the pattern match went.

g1smd: Yes the files have been moved to a new subdirectory. I'd rather redirect them to the new place.