Redirect everything in a subfolder to another folder using .htaccess

I am trying to get the following group of urls to redirect to a single subfolder. I would like it to be one rule to deal with all of the urls because i will have a bunch more.

mysite.com/subfolder/attachmentlibrary/safety/1102000/albums.php?albumId=175339
mysite.com/subfolder/attachmentlibrary/safety/1107778/albums.php
mysite.com/subfolder/attachmentlibrary/safety/1107778/albums.php?albumId=
mysite.com/subfolder/attachmentlibrary/safety/1110600/1101254/albums.php?albumId=175339
mysite.com/subfolder/attachmentlibrary/safety/1113010/1113010-BRO.pdf?url=1113010-BRO&last=1113010&rest=Images/safety&ext=.pdf

All above url’s Redirect to:

mysite.com/store/safety/

I have tried
RewriteRule ^subfolder/attachmentlibrary/safety/([a-z/.]*)$ /store/safety/$1 [R=301,L]

But i can’t get it to work.

Any help would be appreciated. I am new to RewriteRules.

Your path segments have numbers, but you’re only matching letters, slashes and dots.

If your intention was to match anything inside subfolder/attachmentlibrary/safety/, then you may as well use (.*)

RewriteRule ^subfolder/attachmentlibrary/safety/(.*)$ /store/safety/$1 [R=301,L]

I do not need to match anything. I just want the urls to all end up in the “mysite.com/store/safety/” subfolder.

This is a bit contradictory. You don’t want to match everything, but you want “all” URLs to redirect. So let me ask a different way: Are there any URLs under subfolder/attachmentlibrary/safety/ that shouldn’t redirect?

Jeff, I don’t need to match anything. I guess what i am trying to say is i want anything with in the “mysite.com/subfolder/attachmentlibrary/safety/” folder to redirect to “mysite.com/store/safety/”.

Nothing needs to match within the subdirectory. All subfolders and files within “mysite.com/subfolder/attachmentlibrary/safety/” redirect to “mysite.com/store/safety/”.

Am I being punked? :-p

It seems we’re saying the same thing, and post #2 is indeed your answer.

LOL… Jeff you are not being punked. I tried the solution you provided in didn’t work, i kept getting 404’s.

It keeps adding things on the end like ‘1107778/albums.php?albumId=’.

skit,

Jeff is correct regarding your character range definition (lowercase letters not matching the digits) but I loathe inappropriate use of (.*) as it generates loops for the unsuspecting user trying to match EVERYTHING. (.*) does match everything but that’s also the problem as it will continue to match EVERYTHING (including every redirection) unless you can add an escape clause in your code.

Now, as to your problem: You have constant subdirectories up to the 7 digit subdirectory which contains albums.php - except for the last two where there are two 7 digit subdirectories in #4 and {garbage}.pdf in #5.

The “trick” to creating effective redirections is to look for a pattern for mod_rewrite to match and use in a redirection.

… but your code appears to only be attempting to replace the subfolder/attachmentlibrary/ with store/. Is that all you’re after? (I doubt if because of #4 and #5.) If so, go back and use Jeff’s (.*) as it will NOT loop because the redirection is NOT to itself but to the store subdirectory (and the {QUERY_STRING} is unaffected).

If not, I share Jeff’s confusion over exactly what it is you are trying to do. To me, a good specification (“specificity”) is important BEFORE attempting to write mod_rewrite code so please try to be as exact as you can in your next attempt to define your problem.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.