Forum Moderators: phranque

Message Too Old, No Replies

Help needed: 301 from any page in a folder to one specific page

         

icedowl

10:10 pm on Nov 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm changing blogging platforms on one of my sites (MovableType to WordPress) and I've decided to not bring any of my old posts over to the new blog. What I'd like to do is a 301 from any page of the old blog to the main page of the new blog. Currently both blogs are in separate folders, but I want to eventually delete all parts of the old blog. The old blog is spread between two folders.

example of what I want to do:
example.com/folder1/anypage.html =>301 to => example.com/newblog/
example.com/folder2/anypage.html =>301 to => example.com/newblog/

I'm hoping there's a simple way. I've done page to page redirects before but this is more complex to me.

jdMorgan

12:01 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See Apache mod_alias RedirectMatch [httpd.apache.org], and give it a shot. Then post your best-effort code here as a basis for discussion.

Thanks,
Jim

icedowl

12:25 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A shot is about all I can try. This is really Greek to me.

Rewrite on
RedirectMatch (http://example.com/folder1/*)\$ http://www.example.com/newblog/$1
RedirectMatch (http://example.com/folder2/*)\$ http://www.example.com/newblog/$1

Why RedirectMatch instead of RedirectPermanent?

Marcia

12:58 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RedirectPermanent directive [httpd.apache.org]

This directive makes the client know that the Redirect is permanent (status 301). Exactly equivalent to Redirect permanent.

RedirectMatch uses Regular Expressions, the Redirect directive doesn't.

RedirectMatch

This directive is equivalent to Redirect, but makes use of standard regular expressions, instead of simple prefix matching. The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.

jdMorgan

1:22 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you read the descriptions of how the two different directives work? The devil is in the details...

Redirect (and its RedirectPermanent and other flavors) uses prefix-matching. If the specified URL-prefix matches the client-requested URL-path, then any part of the client-requested URL-path that was NOT specified in the Redirect prefix gets appended to the specified new URL prefix.

So this would defeat what you are trying to accomplish, because it would NOT drop the old post names -- It would carry them over.

RedirectMatch, however, does not use prefix-match, it uses regular-expressions pattern-matching, which is much more powerful and allows matches anywhere in the URL-string, not just in the prefix. It also requires that any part of the URL-path to be "copied-over" from the requested URL to the new URL be explicitly stated by using back-references to parenthesized sub-patterns -- $1 through $9, for example, would back-reference the values matching the first through ninth parenthesized sub-patterns.

If this is all Greek, then we'd be pleased to answer your questions about what resources to use to translate it, and perhaps offer advice on the most idiomatic translation for a particular phrase. However, we're not set up to follow you around on your tour and do all the talking for you... That is why we always ask that any thread requesting code include a 'best-effort' attempt. It saves time by letting everyone know what the poster does and does not already grasp, and where the 'tutoring' should be focused.

Reading the Apache documentation is a really good use of your time if you want to run a well-mannered server hosting a well-ranked site... :) It's not easy, reading all that. But it is a lot easier than recovering a site that lost its search ranking because of one little error in the server config code. And that is not an unlikely scenario, it happens all the time.

"Rewrite on" is neither valid nor required. "RewriteEngine on" is a directive for mod_rewrite, which we are not using here (though we could).


RedirectMatch ^/folder1/[^.]+\.html$ http://www.example.com/newblog/
RedirectMatch ^/folder2/[^.]+\.html$ http://www.example.com/newblog/

The regular-expressions pattern in the first line reads, "Match any URL-path starting with a slash, followed by 'folder1' and another slash, followed by one or more characters not equal to a literal period, followed by a literal period, and ending with 'html'." The "^" character, when used at the beginning of a pattern indicates that a matching string must start with the following characters. When used at the beginning of a character [group], it means "Not". So, [^.] means "any character not a period." Outside of [groups], literal periods must be escaped by preceding them with a "\".

Having worked through that example, you could combine those two directives:


RedirectMatch ^/(folder1¦folder2)/[^.]+\.html$ http://www.example.com/newblog/

The only trick is that you must replace the broken pipe "¦" character in that line with a solid pipe character before use; Posting on this forum changes solid pipe characters to broken pipes, making the pattern invalid.

Jim

icedowl

6:07 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim, thank you for your wonderful explanation of the meanings of the pieces of the code. I didn't see anything like that at the Apache pages referenced above, only definitions. I would like to know where to look to find out about the special character combinations that are available. And, do you know if there is a good book available about this subject? I seem to do better with reference books in my hands.

I'll give that code a try, then see if I need further help with it. I will be trying the 2 line version as my folder2 has files ending with ".cgi" instead of ".html".

Sorry to be delayed getting back to this but I had to sleep. Working the graveyard shift makes for strange hours.

icedowl

6:23 am on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yay! It works! Thank you so much. :)

jdMorgan

5:08 pm on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Our Apache Forum Charter contains links to some useful resources, including a regular-expressions tutorial.

The Apache section of the WebmasterWorld library contains links to several useful threads as well.

Look in the technical section of on-line booksellers sites -- There are a decent number of books on Apache and on regular expressions on the market. WIth a list of these, you can go to your local bookstore and see which ones are wrriten in a style that best fits with your preferred way of learning.
Jim

icedowl

6:05 pm on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One quick question. Should I be concerned that this code is giving 302's instead of 301's? This is a permanent switch that I'm making.

jdMorgan

11:05 pm on Nov 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Absolutely! Use a 301 unless you want to nuke your search rankings.

Use RedirectMatch 301 ....

to specify a 301 instead of the default 302. Please look here [httpd.apache.org] and note the status in the Directive syntax.

Jim

icedowl

3:24 am on Nov 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you very much once again! All is as I want it to be now. Now I'm off to see what I can find in the way of a book.