Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect

         

geohei

10:15 am on Jan 1, 2015 (gmt 0)

10+ Year Member



Hi.

I need to redirect http: requests from a directory (and only this directory) in olddomain.com to the same directory in newdomain. This directory contains sub-directories which include files (of course).

So ... the complete content of directory www.olddomain.com/downloads was copied to www.newdomain.com/downloads.

http://www.olddomain.com/downloads/subdir/files.zip
http://www.newdomain.com/downloads/subdir/files.zip


How can I redirect using .htaccess?

Many thanks,

[edited by: phranque at 11:38 am (utc) on Jan 1, 2015]
[edit reason] unlinked urls [/edit]

lucy24

4:31 pm on Jan 1, 2015 (gmt 0)

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



What have you tried so far, and what happened?

Note that htaccess is a location, not a function. Depending on what else you're already doing, the redirect can be done with either mod_alias (Redirect by that name) or mod_rewrite (RewriteRule). In fact, redirecting entire directories is pretty much the textbook case of what mod_alias is good at.

wslade

5:36 pm on Jan 1, 2015 (gmt 0)

10+ Year Member



I like the mod_alias route lucy24 mentions. There are several other ways to do what you want to do. You can do a php redirect:

<?php header http://www.newdomain.com/downloads/subdir/files.zip '); ?> 
Put this in the old file location.

You can also use the refresh html tag to redirect a page.

If SEO is an issue, you should be sure add the appropriate error message.

geohei

8:01 pm on Jan 1, 2015 (gmt 0)

10+ Year Member



This .htaccess in the downloads directory worked.
However I'm not sure if there are no drawbacks.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/downloads/$1 [R=301,L]

phranque

10:22 pm on Jan 1, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, geohei!

your solution looks good - let us know if you have any problems.

lucy24

10:26 pm on Jan 1, 2015 (gmt 0)

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



The quoted RewriteRule will work for all requests all the time. If the rule is located in a directory where absolutely everything is to be redirected, then yes that's fine.

One caveat: By default, RewriteRules are not inherited. So if you have any RewriteRules in your root-level htaccess, requests for material in the /downloads/ directory will ignore these RewriteRules. In particular: If you have RewriteRules ending in [F] (for "get out of my sight, you nasty robot") they will have no effect on the /downloads/ directory. You could add a
RewriteOptions inherit

though this doesn't always work as intended. If you don't have any earlier rules that need to be inherited, you're good to go.

geohei

2:33 pm on Jan 4, 2015 (gmt 0)

10+ Year Member



Ok ... understood.
Many thanks!