Forum Moderators: phranque

Message Too Old, No Replies

How301 redirect all files in one folder on old domain to a new domain?

         

Lorel

7:19 pm on Nov 3, 2011 (gmt 0)

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



I had about 75 files in a folder on one domain and set up a new domain for that topic and need to 301 redirect all those files in that folder to the new site (file names have not changed).

I assume this can be done without setting up a separate 301 for each page in the folder which would make the htaccess file very large for the old site.

I see a discussion from 2007 on how to do this in WebmasterWorld but I don't understand the terminology in the referenced httpd.apache.org mod alias doc. Can someone translate please?

lucy24

7:41 pm on Nov 3, 2011 (gmt 0)

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



Double-check: You're redirecting everything in one folder and nothing from anywhere else?

Using mod_alias it's

Redirect 301 /directoryname http://www.example.com/

But if you have anything at all in your htaccess that uses mod_rewrite*, use

RewriteRule /directoryname/?(.*) http://www.example.com/$1 [R=301,L]

That's assuming you have only one directory with that name, and that you don't have to deal with other issues like with-and-without www or appended port numbers.

The difference in wording is because mod_alias works with exact names and reattaches the rest of the path, while mod_rewrite throws away anything you haven't explicitly captured, and will see partial names.


* g1smd has some boilerplate that you will be seeing shortly.

Lorel

7:15 am on Nov 8, 2011 (gmt 0)

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




Yes. I'm redirecting everything in one folder. But there are other redirects for the main domain (the fix for bogus index and the non-www to www fix).

I've been waiting for *g1smd info. But went ahead and tried the following line in the htaccess file in the old domain and it didn't redirect anything --it threw a 404 (I changed the relevant names of course):

RewriteRule /directoryname/?(.*) http://www.example.com/$1 [R=301,L]

I also tried this line with the same result:

Redirect 301 /directoryname http://www.example.com/

I already have the generic redirect in there (see below) and that works for the home page but not the other pages:

RewriteRule ^directoryname/$ http://www.example.com [R=301,L]

On the new site I have the rewrite for changing non-www to www and that works fine except when that new line is installed.

g1smd

9:12 am on Nov 8, 2011 (gmt 0)

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



Fixing the code...

RewriteRule ^directoryname/index\.(php|html?) http://www.example.com/newfolder/ [R=301,L]
RewriteRule ^directoryname/(.*) http://www.example.com/newfolder/$1 [R=301,L]


OR

RewriteRule ^directoryname/index\.(php|html?) http://www.example.com/ [R=301,L]
RewriteRule ^directoryname/(.*) http://www.example.com/$1 [R=301,L]


Why two rules?

This avoids a double redirect for named index files.

[edited by: g1smd at 9:43 am (utc) on Nov 8, 2011]

lucy24

9:40 am on Nov 8, 2011 (gmt 0)

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



Overlapping...

If you've already got a rewrite, stay with it. Don't introduce a Redirect into the mix. But are all those pages going to be flopping around loose in the new domain, or is there a directory waiting to receive them?

:: pause to whap self upside of head ::

Do the RewriteRule without the leading slash. For some reason, my own server doesn't seem to care. But officially there isn't supposed to be one. The one for the index file alone doesn't have a slash, and you said it works. So if

RewriteRule ^directoryname/$ http://www.example.com [R=301,L]

redirects correctly, it should be possible to expand it to

RewriteRule ^directoryname(/morestuff)?$ http://www.example.com$1 [R=301,L]

Lorel

3:43 pm on Nov 11, 2011 (gmt 0)

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



@g1smd
This worked, Thanks:

RewriteRule ^directoryname/index\.html http://www.example.com/ [R=301,L]
RewriteRule ^directoryname/(.*) http://www.example.com/$1 [R=301,L]

@lucy24

The pages have all been moved to their own domain, not in a directory.

thanks for the help

g1smd

8:06 pm on Nov 11, 2011 (gmt 0)

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



Thanks for confirming the code works.

Three other people asked the same question again today, so this thread will be useful to them.