Forum Moderators: phranque

Message Too Old, No Replies

301 old files to new domain

apache 301 redirects

         

espmartin

2:00 am on Dec 22, 2007 (gmt 0)

10+ Year Member



Hello,

I'm moving a sub-domain to a new FQDN I just bought.

This works for me:

Redirect 301 / http://www.example.org/

But this does not:

Redirect 301 /health-and-diet/puppies-and-worms.php http://www.example.org/health-and-diet/puppies-and-worms/

Instead of redirecting to the directory "puppies-and-worms" - it tries to add the file name:

http://www.example.org/health-and-diet/puppies-and-worms/puppies-and-worms.php

What am I doing wrong?

[edited by: jdMorgan at 2:40 am (utc) on Dec. 22, 2007]
[edit reason] example.org. Please see TOS. [/edit]

espmartin

2:03 am on Dec 22, 2007 (gmt 0)

10+ Year Member



Here's the entire contents of the .htaccess file from the sub-domain:


RewriteEngine On
Redirect 301 / http://www.example.org/
Redirect 301 /dog-blog/ http://www.example.org/
Redirect 301 /health-and-diet/ http://www.example.org/health-and-diet/
Redirect 301 /health-and-diet/puppies-and-worms.php http://www.example.org/health-and-diet/puppies-and-worms/
Redirect 301 /resources/articles.php http://www.example.org/resources/pet-articles/
Redirect 301 /resources/spay.php http://www.example.org/resources/spay-and-neuter/
Redirect 301 /resources/contact.php http://www.example.org/contact/
Redirect 301 /training-articles/ http://www.example.org/training-articles/

The redirects from the old directories to the new directories work, but the ones that have filenames (.php) don't. They all add the filename.php to the redirected URL.

Thanks in advance!

[edited by: jdMorgan at 2:36 am (utc) on Dec. 22, 2007]
[edit reason] example.org [/edit]

Marcia

2:17 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, what you're trying to redirect in your first example isn't a sub-domain it's a subdirectory; in fact what isn't working is trying to redirect a page within the subdirectory to the root of the corresponding /directory/ on the other domain.

It's actually doing what the Redirect directive in mod_alias usually does, even within the same site. You might want to give RedirectMatch a whirl and see if it'll work for you. The RedirectMatch directive supports regular expressions, and is the equivalent, but the Redirect directive (which is what you're now using) doesn't support them.

[stanford.edu...]

Syntax:

RedirectMatch [status] regex URL

It can't hurt to give it a try, it's just another tool that's available and they're all different.

From the forum library:

Regular Expressions [webmasterworld.com]

I know, I know: arrggghhhhh! :)

[edited by: Marcia at 2:32 am (utc) on Dec. 22, 2007]

jdMorgan

2:40 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code above would probably work if you put the most-specific directives first -- i.e. put the Redirect including the .php filename ahead of the one for that same directory.

The Redirect directive uses prefix-matching. Therefore, your less-specific whole-directory Redirect directives will always be invoked because they come first in your posted code, and the more-specific per-file ones won't ever be invoked.

[added] ... and the domain redirect --currently your first Redirect directive-- should be the last one, because it will match *all* URLs, and redirect them to the same URL on the new domain. Always order your redirects and rewrites from most-specific (longest URL-path) to least-specific. [/added]

Jim

[edited by: jdMorgan at 2:44 am (utc) on Dec. 22, 2007]

Marcia

2:50 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code above would probably work if you put the most-specific directives first -- i.e. put the Redirect including the .php filename ahead of the one for that same directory.

Will it work that same way within the same site also, using specific filenames first? That's been giving a big problem moving things from one directory to another on the same site, with the filenames being carried forward when it isn't wanted.

espmartin

3:15 am on Dec 22, 2007 (gmt 0)

10+ Year Member



Thanks all,

I'm trying the order switching of rules now.

And I should have been more clearer on the whole sub-domain thing. Yes, these are sub-folders, but they are sub-folders on one of my sub-domains :)

espmartin

3:24 am on Dec 22, 2007 (gmt 0)

10+ Year Member



It's all working now (YES!) - but not this one redirect:

Redirect 301 /dog-food-recall/ http://www.example.org/resources/dog-food-recall/

Here's the newly ordered .htaccess:

RewriteEngine On
Redirect 301 /health-and-diet/puppies-and-worms.php http://www.example.org/health-and-diet/puppies-and-worms/
Redirect 301 /health-and-diet/ http://www.example.org/health-and-diet/
Redirect 301 /resources/articles.php http://www.example.org/resources/pet-articles/
Redirect 301 /resources/spay.php http://www.example.org/resources/spay-and-neuter/
Redirect 301 /resources/contact.php http://www.example.org/contact/
Redirect 301 /training-articles/ http://www.example.org/training-articles/
Redirect 301 /dog-food-recall/ http://www.example.org/resources/dog-food-recall/
Redirect 301 /dog-blog/ http://www.example.org/
Redirect 301 / http://www.example.org/

Thanks all of you! Any thoughts on this last one?

[edited by: jdMorgan at 4:03 am (utc) on Dec. 22, 2007]
[edit reason] example.org. Please see Terms of Service. [/edit]

espmartin

3:27 am on Dec 22, 2007 (gmt 0)

10+ Year Member



Actually, I just removed the new "dog-food-recall" page from under that resource directory, and it works now.

It's the wrong path in the .htaccess for it, but at least
it's working :)

Marcia

3:49 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I just tried switching sequences, and got it working with Redirect with using the specific filename first. Wish I'd asked last year instead of struggling and never getting it right

But here's what I just tried for redirecting all of the individual files in a /directory/ to a subdirectory on another site without specifying any individual pages, using just one line of code:

RedirectMatch ^/directoryname/(.*)\.html http://www.example.com/subdirectory/

After three 500 Internal Server Errors, it's working just fine, redirecting for all the files within the directory. They all go to the root of the subdirectory on the other site.

Added: That gives a 302. This way gives a 301:

RedirectMatch 301 ^/directoryname/(.*)\.html http://www.example.com/subdirectory/

[edited by: Marcia at 4:03 am (utc) on Dec. 22, 2007]

jdMorgan

4:02 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will be just a bit faster -- avoid ".*" in the middle of patterns whenever possible.

RedirectMatch 301 ^/directoryname/([^.]+)\.html$ http://www.example.com/subdirectory/

Jim

[edited by: jdMorgan at 4:07 am (utc) on Dec. 22, 2007]

Marcia

5:12 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks much! This will all be very handy to know and use.