Redirect ALL files of a directory to a new destination (htaccess)

Hi guys,

I know how to redirect a single file to a new destination using htaccess:

Redirect 301 /mag3.php http://www.xyz.de

Now I want to set up a redirect for ALL files in a certain folder (that does no longer exist on this server) to point to a new destination.

So that visits to

www.xyz.de/oldfolder/2312323.html OR
www.xyz.de/oldfolder/2387777.html OR
www.xyz.de/oldfolder/7342622.html and so on

are all redirected to

www.xyz.de/newfolder/index.php

Any ideas?

Cheers
Kai

You should just be able to use something like this:

RewriteEngine On
RewriteRule ^oldfolder/(.*) newfolder/index.php [R=301,L]

hi thanks for your help, but it doesn’t seem to work:

http://www.ausmag.de/australien_faq/fdfd.htm

guys,

RewriteEngine On
RewriteRule ^oldfolder/(.*) newfolder/index.php [R=301,L]

Close but …

RewriteEngine on
RewriteRule ^/?oldfolder/(.*)$ newfolder/$1 [R=301,L]

The obvious difference (other than keeping both versions of Apache happy with the ^/?) is the use of the $1 variable which was captured with the EVERYTHING atom in the regex (which IS a proper use).

Regards,

DK

Geez, I forgot my endline metacharacter ($), and after an everything atom too…yikes. I’ve got to remember that “^/?” portion in the future…

If Kai is really hoping to redirect ANY file in oldfolder directly to index.php, couldn’t you abandon the backreference portion and just go with this?

RewriteEngine on
RewriteRule ^/?oldfolder/.*$^ newfolder/index.php [R=301,L]

wb,

Except for that .*$^, none of which is needed, you’ve got it. I think that most people would redirect to the same file in the new directory, though.

Regards,

DK

hi guys,

i don’t know what I’m doing wrong, but it’s still not working.
This is what my .htaccess looks like at the moment:

php_value memory_limit 16M

Redirect 301 /bestellen.php http://www.ausmag.de
Redirect 301 /daspaket.php http://www.ausmag.de
Redirect 301 /mag1.php http://www.ausmag.de
Redirect 301 /mag2.php http://www.ausmag.de
Redirect 301 /mag3.php http://www.ausmag.de
Redirect 301 /mag4.php http://www.ausmag.de
Redirect 301 /australien.php http://www.ausmag.de
Redirect 301 /partner/partnerdoor.php http://www.ausmag.de
RewriteEngine On
RewriteRule ^/?australien_faq/(.*)$ australien-faq/$1 [R=301,L]

Rewriting is working on this server!

Strange… :-/

br,

Since the RewriteRule is last, are the Redirects taking effect before you get to your URI - which is WHAT?

Regards,

DK

hi, sorry but I don’t understand your last post?!

The redirects are working fine. What do I have to do to get the Rewriting rule working?

Thanks a lot!

Kai

br,

What is the URI that you’re testing with (so we can see where it’s failing with the .htaccess you provided)?

Regards,

DK

oh… ok sorry…

well the old URL was

www.ausmag.de/australien_faq/1_433.html (for example)

and I want all of them to be pointed at

www.ausmag.de/australien-faq/

(please note the underscore (old) and the hyphen (new).

Thanks for your help!!

Kai

kai,

There is NOTHING wrong with your regex

RewriteRule ^/?australien_faq/(.*)$ australien-faq/$1 [R=301,L]

Would you please run the quick test I have in my signature article so we can both be sure that mod_rewrite’s working properly.

Regards,

DK

sure… but what is a signature article? :wink: you don’t seem to have a signature at the momen. What should i do?

Thanks for your help!

Cheers,
Kai

kai,

That’s the “Article” link in[INDENT]David K. Lynn
Hosting Team (Apache) Mentor
Data Koncepts
Article: Setup, Config, Test & Write mod_rewrite regex w/Examples[/INDENT]
Regards,

DK

hi DK,

thanks for the link! I’ve just tested it and it worked fine.

Ok, I don’t know why it wasn’t working, but now it is. I changed the .htaccess from this

php_value memory_limit 16M
RewriteEngine On
RewriteRule ^/?australien_faq/(.*)$ australien-faq/$1 [R=301,L]
Redirect 301 /bestellen.php http://www.ausmag.de
Redirect 301 /daspaket.php http://www.ausmag.de
Redirect 301 /mag1.php http://www.ausmag.de
Redirect 301 /mag2.php http://www.ausmag.de
Redirect 301 /mag3.php http://www.ausmag.de
Redirect 301 /mag4.php http://www.ausmag.de
Redirect 301 /australien.php http://www.ausmag.de
Redirect 301 /partner/partnerdoor.php http://www.ausmag.de/ausmag/partner/partnerdoor.php

to this

php_value memory_limit 16M

RewriteEngine On

RewriteRule ^/?australien_faq/(.*)$ http://www.ausmag.de/australien-faq/ [R=301,L]

Redirect 301 /bestellen.php http://www.ausmag.de
Redirect 301 /daspaket.php http://www.ausmag.de
Redirect 301 /mag1.php http://www.ausmag.de
Redirect 301 /mag2.php http://www.ausmag.de
Redirect 301 /mag3.php http://www.ausmag.de
Redirect 301 /mag4.php http://www.ausmag.de
Redirect 301 /australien.php http://www.ausmag.de
Redirect 301 /partner/partnerdoor.php http://www.ausmag.de/ausmag/partner/partnerdoor.php

Maybe it just needed the whole URL in there?
Anyway, it’s working now: www.ausmag.de/australien_faq/12345.html

Thanks again for your help!!! Much appreciated.

Kai

kai,

From the change in your RewriteRule, I’d have said that australien-faq/ was not in the DocumentRoot.

Oh, well, glad you’ve got it sorted.

Regards,

DK